Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all set-cookie headers from node-fetch?

In one of the projects I started to work on the original owner uses node-fetch for http request. node-fetch provides res.headers.get('set-cookie') but it only returns one of the set-cookie headers. (Usually you can have multiple set-cookie in a response header).

Without abandoning node-fetch, is it possible to get all set-cookie headers from the response?

like image 666
Aero Wang Avatar asked Oct 27 '25 09:10

Aero Wang


2 Answers

res.headers.raw() returns a map of header names to arrays of header values; so something like:

res.headers.raw()['set-cookie'].each(c => console.log(c))

Will do the trick, I believe.

like image 82
womble Avatar answered Oct 28 '25 22:10

womble


The Headers object has a get method that returns an Array.

You must be using an older version of node-fetch. In 2.x, res.headers.get should be compliant with the spec.

res.headers.get('set-cookie'); // Array

If you can't upgrade to 2.x for some reason, you can use the non-standard getAll method instead.

like image 41
Jacob Avatar answered Oct 28 '25 23:10

Jacob