Search code examples
kdb

Kdb/q example of http client with authentication


Is it possible to use Kdb+ http client to access pages protected by login? I am using https://github.com/KxSystems/cookbook/blob/master/yahoo.q as example of basic GET/POST. Does anyone have an example how to extract a cookie and use it in the following requests?


Solution

  • It is probably a bit crude, but the following will extract headers from an http, then cookies, parse and return as a dictionary:

    x:"HTTP/1.0 200 OK\r\nContent-type: text/html\r\nSet-Cookie: theme=light\r\nSet-Cookie: sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT\r\n\r\n";
    
    
    left:{(first y ss x)#y};
    vs1:{{(y#x;(count[z]+y)_x)}[y;;x](first y ss x)};
    headers:{{(`$x[0];x[1])} flip vs1[": "] each 1_"\r\n" vs left["\r\n\r\n"]x};
    cookies:{(!). {(`$x[0];x[1])} flip vs1["="] each {x[1]@where x[0]=`$"Set-Cookie"} x};
    
    cookies headers[x]