HTTP Cookies
reQ has built-in support for HTTP cookies; when a response is received with a
Set-Cookie
header, the relevant cookie will be stored in .cookie.jar
,
and then used automatically for future requests to the relevant host.
Expired cookies will be ignored, although currently the Max-Age
attribute is
not taken into account; this will be rectified in a future update.
It's also possible to use the .req.addcookie
function, as
demonstrated in the Advent of Code example.
Setting cookies
q).req.get["http://httpbin.org/cookies/set?abc=123&def=456";()!()];
-- REQUEST --
GET /cookies/set?abc=123&def=456 HTTP/1.1
Host: httpbin.org
Connection: Close
User-Agent: kdb+/3.5
Accept: */*
-- RESPONSE --
HTTP/1.1 302 FOUND
Connection: close
Server: gunicorn/19.8.1
Date: Mon, 04 Jun 2018 22:49:15 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 223
Location: /cookies
Set-Cookie: abc=123; Path=/
Set-Cookie: def=456; Path=/
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: 1.1 vegur
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="/cookies">/cookies</a>. If not click the link.
-- REQUEST --
GET /cookies HTTP/1.1
Host: httpbin.org
Connection: Close
User-Agent: kdb+/3.5
Accept: */*
Cookie: abc=123; def=456
-- RESPONSE --
HTTP/1.1 200 OK
Connection: close
Server: gunicorn/19.8.1
Date: Mon, 04 Jun 2018 22:49:16 GMT
Content-Type: application/json
Content-Length: 38
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: 1.1 vegur
{"cookies":{"abc":"123","def":"456"}}
q).cookie.jar
host path name | val expires maxage secure httponly samesite
------------------------| ---------------------------------------------
"httpbin.org" "/*" "abc"| "123" 0 0
"httpbin.org" "/*" "def"| "456" 0 0
Deleting cookies
Cookies can be deleted or overwritten in the normal fashion, and expired cookies will be ignored.
q).req.get["http://httpbin.org/cookies/delete?abc";()!()];
-- REQUEST --
GET /cookies/delete?abc HTTP/1.1
Host: httpbin.org
Connection: Close
User-Agent: kdb+/3.5
Accept: */*
Cookie: abc=123; def=456
-- RESPONSE --
HTTP/1.1 302 FOUND
Connection: close
Server: gunicorn/19.8.1
Date: Mon, 04 Jun 2018 22:50:48 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 223
Location: /cookies
Set-Cookie: abc=; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: 1.1 vegur
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="/cookies">/cookies</a>. If not click the link.
-- REQUEST --
GET /cookies HTTP/1.1
Host: httpbin.org
Connection: Close
User-Agent: kdb+/3.5
Accept: */*
Cookie: def=456
-- RESPONSE --
HTTP/1.1 200 OK
Connection: close
Server: gunicorn/19.8.1
Date: Mon, 04 Jun 2018 22:50:48 GMT
Content-Type: application/json
Content-Length: 26
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: 1.1 vegur
{"cookies":{"def":"456"}}
q).cookie.jar
host path name | val expires maxage secure httponl..
------------------------| ---------------------------------------------------..
"httpbin.org" "/*" "abc"| "" 1970.01.01T00:00:00.000 0 0 0 ..
"httpbin.org" "/*" "def"| "456" 0 0 ..
Compatibility with cURL/Netscape cookie jar
reQ has support for reading & writing the cookie jar in the format initially developed by Netscape and utilised in cURL. See https://curl.haxx.se/docs/http-cookies.html for more details on the format. This support allows for transition from a cURL based workflow.
Reading cookie jar
Read cookie jar with .req.readjar
:
jonny@kodiak ~/git/req (master) $ more tests/cookiejar
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by reQ! Edit at your own risk.
httpbin.org TRUE / FALSE 1143149359 abc 123
httpbin.org FALSE /example TRUE 0 def 123
jonny@kodiak ~/git/req (master) $ q
KDB+ 3.5 2018.04.25 Copyright (C) 1993-2018 Kx Systems
l64/ 4(16)core 7360MB jonny kodiak 127.0.1.1 EXPIRE 2019.05.21 jonathon.mcmurray@aquaq.co.uk KOD #4160315
q)\l req.q
q).req.readjar`:tests/cookiejar
host path secure expires name val httponly maxage samesite
----------------------------------------------------------------------------------------------
"*httpbin.org" "/*" 0 2006.03.23T21:29:19.000 "abc" "123" 0
"httpbin.org" "/example*" 1 "def" "123" 0
Writing cookie jar
Write cookie jar with .req.writejar
:
jonny@kodiak ~/git/req (master) $ q
KDB+ 3.5 2018.04.25 Copyright (C) 1993-2018 Kx Systems
l64/ 4(16)core 7360MB jonny kodiak 127.0.1.1 EXPIRE 2019.05.21 jonathon.mcmurray@aquaq.co.uk KOD #4160315
q)\l req.q
... // build up reQ cookiejar
q).cookie.jar
host path name | val expires maxage secure httponly samesite
--------------------------------| -------------------------------------------------------------
"*httpbin.org" "/*" "abc"| "123" 2006.03.23T21:29:19.000 0 0
"httpbin.org" "/example*" "def"| "123" 1 0
q).req.writejar[`:jar].cookie.jar
`:jar
q)\\
jonny@kodiak ~/git/req_bk (master) $ more jar
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by reQ! Edit at your own risk.
httpbin.org TRUE / FALSE 1143149359 abc 123
httpbin.org FALSE /example TRUE 0 def 123