POST with Body Not Passing Cookies
Asked Answered
C

1

17

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();
Cedillo answered 14/12, 2016 at 12:10 Comment(4)
Can you show your initialization of cookieJar? Are you using setCookie() or setCookieSync()?Phlegmatic
@JeremyHarris, I have added that. I'm not using setCookie anywhere.Cedillo
I suspect that serialization is the problem. try custom serializer and post.Trustee
Didi you get that working?Elseelset
T
5

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))
Trustee answered 19/12, 2016 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.