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.

Axios doesn't send cookies with POST and data

See original GitHub issue

Summary

I was beating my head off of the issue reported in #191 for awhile but then I noticed something. Post requests WITHOUT data work fine. Post requests WITH data seem to overwrite some config settings.

#### Context

Specifically for me, all my AJAX requests (which are CORS-protected) need a cookie to get past my authentication provider. Given that, I had set withCredentials to true in my axios instance. Everything was fine for GET and data-less POST requests. But as soon as I tried to post some data, I got hit with CORS errors again. Upon inspection, for some reason Axios stops sending the cookie header (and thus the auth token) when I specify data. The lack of the cookie means the server rejects the request (returning a 302 in my case) which the browser reports as a CORS failure, oddly enough.

I saw the message from @mike-robertson on #191 about using json.stringify. However, my data must be urlencoded. I used the query-string package to encode the parameters directly. It worked after that!

In summary, given:

import axios from 'axios';
import queryString from 'query-string';

const api = axios.create({
	withCredentials: true
});

params = {
   foo: 'bar',
   baz: 3
};

then the following will not send the cookie header.

api.post('bluh', params);

but these will:

api.post('bluh');
api.post('bluh', queryString.stringify(params));

This seems like a pretty major problem and should at least be mentioned in the docs.

  • axios version: v0.16.1
  • Environment: node v7.6.0 on KDE Neon
  • Chrome info:
Google Chrome	58.0.3029.81 (Official Build) (64-bit)
Revision	ac0bae59f0aa5b391517e132bf172144b1939333-refs/branch-heads/3029@{#746}
OS	Linux
JavaScript	V8 5.8.283.32

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:38
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

126reactions
jaspersorriocommented, Jun 20, 2017

I had the same problem with post not sending cookies!

I solved my problem by doing

axios("http://mysite.com/api/things/", {
  method: "post",
  data: someJsonData,
  withCredentials: true
})
15reactions
toyflishcommented, May 2, 2017

I had a similar issue, doing a a post-request same origin axios with default configuration sent the auth-cookie, same setup as cors from other domain axios did not send the auth-cookie until I set the configuration to axios.defaults.withCredentials = true. Would be better to have same behaviour on both types of request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make Axios send cookies in its requests automatically
For anyone who may be wondering why their GET request says true for isAuthenticated, but their POST request does not, your POST must...
Read more >
axios get cookies from response - You.com | The AI Search ...
Passing cookies with axios Axios is a popular library for making HTTP requests because the API is simple yet powerful. With axios, you...
Read more >
How to Pass Cookies with Fetch or Axios Requests - Sabe.io
However, cookies are not sent by default when you make client-side requests to the server. In this post, we're going to learn how...
Read more >
Axios vs. fetch(): Which is best for making HTTP requests?
Basic syntax · To send data, fetch() uses the body property for a post request to send data to the endpoint, while Axios...
Read more >
axios does not send cookies Code Example
Queries related to “axios does not send cookies” · axios set cookie · axios send cookie · axios get cookie from response ·...
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