Search code examples
httpkdb

KDB: Difference between .Q.hp/.Q.hg and the built in HTTP request. And do either persist/Keep alive?


Its seems a http get can be performed using either .Q.hg or using the built in HTTP request like

`:http://host:port "string to send as HTTP method etc" (from https://code.kx.com/q/kb/programming-examples/)

Is there any difference?

And do either persist/keep-alive by default?

Thank you


Solution

  • Using .Q.hg allows you to use a string which is formatted in a way that is consistent with a web-based url request, .e.g for requesting some csv data from a server:

    t:.Q.hg`$":http://www.website.com/report1/format=csv&cols=sym&cols=price&date=20200630";
    /the resulting string contains the data only (no metadata/headers) and can be parsed directly
    ("SF";1#csv)0:t
    

    The GET equivalent is not like a browser url, however it does return the metadata/headers (which in turn makes it messier to parse), e.g.

    t:(hsym`$"http://www.website.com") "GET /report1/format=csv&cols=sym&cols=price&date=20200630 HTTP/1.1\r\nhost:www.website.com\r\n\r\n";
    /result looks like
    "HTTP/1.1 200 OK\r\nDate: Fri, 03 Jul 2020 14:46:33 GMT\r\nContent-Type: application/txt\r\nContent-Length: 1345\r\nConnection: keep-alive ...."
    /parsed using something like (strip away metadata to get to the data)
    ("SF";1#csv)0:_[;t]3+first t ss "\n\r\n"
    

    The resulting metadata/header shows "Connection: keep-alive" in my example that I've just tested so perhaps that's the default? I'm not 100% on that.

    .Q.hg also has the advantage of being compatible with HTTPS and making use of proxies as per the documentation: https://code.kx.com/q/ref/dotq/#qhg-http-get