question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to read set-cookie header in node

See original GitHub issue

First sanity check:

❯ curl -I "https://httpbin.org/cookies/set?a=1"
HTTP/1.1 302 FOUND
Connection: keep-alive
Server: meinheld/0.6.1
Date: Tue, 13 Jun 2017 15:25:30 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 223
Location: /cookies
Set-Cookie: a=1; Path=/
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0.00121402740479
Via: 1.1 vegur

Compare with Axios:

import Axios from "axios"

it("axios supports set-cookie", () =>
  Axios.request({
    method: "get",
    url: "https://httpbin.org/cookies/set?a=1"
  }).then(res => {
    expect(res.headers["set-cookie"]).toMatch(/a=1/)
  }))
  ● axios supports set-cookie

    expect(string)[.not].toMatch(expected)

    string value must be a string.
    Received: undefined

Releatd https://github.com/mzabriskie/axios/issues/295. Am I missing something?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
only-clichescommented, Jun 14, 2017

Actually just did some testing, that specific site sets the header and does a 302 redirect, Axios isn’t setting the cookie before following the redirect, meaning you just get sent to https://httpbin.org/cookies/ with no cookie set.

You can see the cookie with this code:

const Axios = require("axios").default;

Axios.request({
    method: "get",
    url: "https://httpbin.org/cookies/set?a=1",
    maxRedirects: 0,
    validateStatus: function(status) {
        return status >= 200 && status < 303;
    },
}).then(res => {
    console.log(res.headers["set-cookie"]);
});
4reactions
samayocommented, Aug 16, 2017

This is still an issue that we ought to discuss about. I just sent a GET request to http://httpbin.org/headers as:

var post = axios('http://httpbin.org/headers', {
  method: 'GET',
  mode: 'no-cors',
  data: some.data,
  withCredentials: true
})

And I am getting all the responses, except for the cookie.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't get set-cookie from header in response with NodeJS
I think you are requesting two different URLs/requests. wget is seeing text/html and other headers, whereas the node requests are seeing ...
Read more >
axios cookie header not working - You.com | The AI Search ...
As it seems, it is not possible to read the Set-Cookie header via Axios, ... cookie value not being set in the request...
Read more >
Cookies, document.cookie - The Modern JavaScript Tutorial
Cookies are usually set by a web-server using the response Set-Cookie HTTP-header. Then, the browser automatically adds them to (almost) ...
Read more >
set-cookie-parser - npm
Parses set-cookie headers into objects. ... Start using set-cookie-parser in your project by running `npm i set-cookie-parser`.
Read more >
Understanding Cookies and Implementing them in Node.js
//a get route for adding a cookie app.get('/setcookie', (req, res) => { res ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found