Can axios set cookies?
See original GitHub issueI’m trying to set a cookie with a response header like so:
Set-Cookie:connect.sid=s%3AvidaO7FJ_U-Fx77zGEAozIizc4eddq8P.wkkigPnMdYNo8SttRYplaI1%2FGL56vUVDgUK3UzoI6DI; Domain=subdomain.ofmywebsite.com; Path=/; Expires=Sun, 04 Mar 2018 15:14:18 GMT
Client side code:
axios.defaults.withCredentials = true;
axios('domain.ofmyserver.com/api/login', {
method: 'post',
data:{
email: this.state.email,
password: this.state.password
},
})
But no error is given and no cookie is set. What am I missing here? When I go directly with the browser to the server domain it will set a cookie. But with a POST request in axios it simply refuses it without any error.
Context
- axios version: 0.17.1
- Environment: Chrome, safari, firefox
- express server with express-session 1.15.6
Issue Analytics
- State:
- Created 6 years ago
- Reactions:8
- Comments:9
Top Results From Across the Web
How to set cookies when send a request in node ? · Issue #943
The cookies need to be passed into the headers object. Axios.request({ url: "http://example.com" ...
Read more >Does Axios support Set-Cookie? Is it possible to authenticate ...
Yes you can set cookies by Axios. The cookies need to be passed into the headers object. You can send cookies in a...
Read more >Handling cookies with axios - Medium
After researching, I found out that we cannot set Cookie as header according to new W3C rules, owing to security reasons.
Read more >Pass cookies with axios or fetch requests - Code with Hugo
Pass cookies with requests in axios In axios, to enable passing of cookies, we use the withCredentials: true option.
Read more >axios get cookie from request - You.com | The AI Search ...
If you are making a same origin request you could set it via document.cookie , otherwise you will have to make an HTTP...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If anyone still has this problem and reads this: double check that you’re setting
domain
andpath
from the server. I see that the creator of this thread did that, but I hadn’t and was going crazy over why it didn’t work. Withfetch
instead ofaxios
, it worked without setting those properties, butaxios
needed them apparently.Having tons of trouble with this.
Domain
andPath
is being set as @ninaolo said:The
Access-Control-Allow-Origin
andAccess-Control-Allow-Credentials
headers have also been set (which can cause issues with CORS). I’ve also tried disabling CORS on the server, the same issue exists.The
withCredentials
setting has also been set:I’ve also tried passing
withCredentials
to the request itself (in case the default setting didn’t work):Yet this still doesn’t seem to be setting the cookie and I don’t know why. The exact same endpoints work in Postman (it sets the cookie from the
Set-Cookie
header), yet in the browser it’s not working.I also tried the same thing with
fetch()
and it worked in Chrome, it shows it in the devtools storage section. Except that when I refresh the page, the cookie disappears. And in Firefox it didn’t even work at all.So this issue seems to not be specific to axios (I initially thought it was). Does anyone know what could be wrong?