Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST with Body Not Passing Cookies

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))

Initialization of Cookie Jar

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();
like image 1000
Mike Perrenoud Avatar asked Dec 14 '16 12:12

Mike Perrenoud


People also ask

Are cookies sent with POST requests?

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.

How do I send cookies in a post request?

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.

Why are cookies not sent?

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.

Are cookies sent with every response?

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.


1 Answers

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))
like image 102
Thaadikkaaran Avatar answered Oct 08 '22 09:10

Thaadikkaaran