How do I save cookies from an HTTP response so I can send them in subsequent HTTP requests?

This topic was automatically generated from Slack. You can find the original thread here.

Rafael Jafferali : Hello, I have some trouble managing cookies with axios. I am trying to collect some data from a website but, in order to do this, I first have to send a post request which returns some cookies, then send another request through a get request.

In Python it is as easy as this :

import requests
s = requests.Session()
s.headers.update({"Content-Type" : "application/x-www-form-urlencoded"})
r = s.post("https://iubel.just.fgov.be/moteur/formulaire", data=SEARCH_DATA)
result = s.get("https://iubel.just.fgov.be/moteur/resultats")
# Process result.text

But everything I try with axios cannot reproduce this. As suggested on some forums, I tried axios.defaults.withCredentials = true but by itself it seems insufficient to make axios retain the cookies received from the first request. I also tried manually retrieving the cookie sent by the first request and using it for the second request with headers: {"Cookie": "..."} but to no avail.

Would you have any suggestion, or is Python truly superior to Node.js in this respect?

Thank you in advance for your help!

Dylan Sather (Pipedream) : if you’re not set on using axios, I have a got example for you:

const {promisify} = require('util');
const got = require('got');
const {CookieJar} = require('tough-cookie');
const cookieJar = new CookieJar();
const setCookie = promisify(cookieJar.setCookie.bind(cookieJar));
await setCookie(`cookie_name=${cookie_value}`, 'https://your-domain.com');
await got('https://your-domain.com', {cookieJar} )

Dylan Sather (Pipedream) : technically you can always pass the cookie data in headers.cookie in any Node HTTP client, but I find this a little more elegant

Dylan Sather (Pipedream) : I’m not sure why that didn’t work for you - feel free to share your workflow if you’d like me to take a look and if the cookieJar / got approach doesn’t work

Rafael Jafferali : Thank you very much for your quick answer. Eventually I found superagent which seems even more easier than got to save cookies during a session, see SuperAgent — elegant API for AJAX in Node and browsers under “Saving cookies”. Thanks for your help anyway!

Dylan Sather (Pipedream) : That’s great! npm is amazing :slight_smile: