Anon21
Anon21

Reputation: 3073

Making an http request using NodeJS and access the returned cookies

I am querying a remote API using NodeJS. I am currently using Axios to make the request, but I am willing to use another package if required.

Using NodeJS, I make a request to a remote API.

Axios.post("http://remote.api/getCookie")
  .then(value => {
    console.log(value);
  });

The API returns a number of cookies (this can be seen in the spec, and when I test it in a browser). How can I access these cookies from the value returned by Axios.

Upvotes: 0

Views: 44

Answers (1)

Jb31
Jb31

Reputation: 1391

Just get them from the Set-Cookie header:

Axios.post("http://<url>").then(response => {
  const cookies = response.headers["set-cookie"];
  // do whatever you want
}

You can then parse the header by yourself or use a library like cookie or set-cookie-parser

Upvotes: 1

Related Questions