简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM