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
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.
public void addCookie(Cookie ck):method of HttpServletResponse interface is used to add cookie in response object.
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.
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.
If you need an array:
response.headers.raw()['set-cookie']
Updating Rachel's Answer.
You need to use .get()
instead of .getAll()
now since version 2.
response.headers.get('set-cookie')
(Source)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With