Axios doesn't send cookies with POST and data
See original GitHub issueSummary
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:
- Created 6 years ago
- Reactions:38
- Comments:15 (1 by maintainers)
I had the same problem with post not sending cookies!
I solved my problem by doing
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.