3

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?

Aero Wang
  • 8,382
  • 14
  • 63
  • 99

2 Answers2

3

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.

womble
  • 12,033
  • 5
  • 52
  • 66
1

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.

Jacob
  • 77,566
  • 24
  • 149
  • 228