POST with Body Not Passing Cookies

1.6k views Asked by At

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();
1

There are 1 answers

0
Thaadikkaaran On BEST ANSWER

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