I'm using the axios-cookiejar-support library.
I have a POST that contains a body, and for some reason, the Cookies aren't getting injected into the request. What did I do wrong here:
return axios
.post(
urlJoin(
config.portal.url,
'Account/Register'),
{
UserName: "[email protected]",
UserFirstName: "First Name",
UserLastName: "Last Name",
Email: "[email protected]",
Password: "...",
ConfirmPassword: "..."
},
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err))
The weird part is, if I perform a GET against the same endpoint the Cookies get passed:
return axios
.get(
urlJoin(
config.portal.url,
'Account/Register'),
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err));
Also, if I perform a POST without a body, they get passed:
.post(
urlJoin(
config.portal.url,
`Account/LoginApi?UserName=${config.portal.userName}&Password=${config.portal.password}`),
null,
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err))
import axios from 'axios'
import axiosCookieJarSupport from '@3846masa/axios-cookiejar-support'
import tough from 'tough-cookie'
import urlJoin from 'url-join'
const config = require('config');
import { TEST_STATUS_TYPES, TEST_TASK_TYPES } from '../constants/testsConstants'
axiosCookieJarSupport(axios);
const cookieJar = new tough.CookieJar();
Cookies are sent with every request, so they can worsen performance (especially for mobile data connections). Modern APIs for client storage are the Web Storage API ( localStorage and sessionStorage ) and IndexedDB.
To send cookies to the server, you need to add the "Cookie: name=value" header to your request. To send multiple Cookies in one cookie header, you can separate them with semicolons. In this Send Cookies example, we are sending HTTP cookies to the ReqBin echo URL.
If the server doesn't allow credentials being sent along, the browser will just not attach cookies and authorization headers. So this could be another reason why the cookies are missing in the POST cross-site request.
Generally, you don't need to set the cookie on each and every response. The browser already has the cookie and will continue sending it to the server as long as it is still valid.
As I commented, I suspect the serialization part. Because when you pass your data as an query string, it works as you expected. So try like this
var qs = require('qs');
return axios
.post(
urlJoin(
config.portal.url,
'Account/Register'),
qs.stringify({
UserName: "[email protected]",
UserFirstName: "First Name",
UserLastName: "Last Name",
Email: "[email protected]",
Password: "...",
ConfirmPassword: "..."
}),
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With