Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why node-fetch cannot retrieve cookies from response headers

Tags:

cookies

fetch

I am using node-fetch to login a website. The site's response has cookies set which i can observe through response.headers. But when i try to response.headers.get('set-cookie'), it just shows part of the cookie array while response.headers._headers['set-cookie'] works fine... If set-cookie is forbidden in fetch API, why I still see it in response headers?

2020-05-26 updated: Refer to the latest version: https://github.com/node-fetch/node-fetch/blob/master/src/headers.js#L152

like image 671
hjl Avatar asked Nov 20 '15 17:11

hjl


People also ask

How do I get cookies from response headers?

Just set the Set-Cookie header in the response from the server side code. The browser should save it automatically. As a developer, you may be able to inspect the value of the cookies using "Developer Tools". And the same cookie will be sent in subsequent requests to the same domain, until the cookie expires.

Can we add cookies using Response object?

public void addCookie(Cookie ck):method of HttpServletResponse interface is used to add cookie in response object.

How do I access response cookies in react?

Now, inside your React component, you can access the cookie by using a useCookies() hook. The cookies object is holding the all cookies, you have created in your app. In class-based components, you can get the cookie by using a withCookies() higher-order component.

What does set-cookie from response header do?

The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.


2 Answers

If you need an array:

response.headers.raw()['set-cookie']
like image 96
pykiss Avatar answered Oct 29 '22 16:10

pykiss


Updating Rachel's Answer.

You need to use .get() instead of .getAll() now since version 2.

response.headers.get('set-cookie')

(Source)

like image 32
artless boy Avatar answered Oct 29 '22 15:10

artless boy