POST 且 Body 未传递 Cookie
POST with Body Not Passing Cookies
我正在使用 axios-cookiejar-support 库。
我有一个包含 body 的 POST,但出于某种原因,Cookie 没有被注入到请求中。我这里做错了什么:
return axios
.post(
urlJoin(
config.portal.url,
'Account/Register'),
{
UserName: "testing_engine@test.com",
UserFirstName: "First Name",
UserLastName: "Last Name",
Email: "testing_engine@test.com",
Password: "...",
ConfirmPassword: "..."
},
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err))
奇怪的是,如果我对同一端点执行 GET,Cookie 就会通过:
return axios
.get(
urlJoin(
config.portal.url,
'Account/Register'),
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err));
此外,如果我执行 POST 而没有 body,他们将通过:
.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();
正如我评论的那样,我怀疑序列化部分。因为当您将数据作为查询字符串传递时,它会按预期工作。所以像这样尝试
var qs = require('qs');
return axios
.post(
urlJoin(
config.portal.url,
'Account/Register'),
qs.stringify({
UserName: "testing_engine@test.com",
UserFirstName: "First Name",
UserLastName: "Last Name",
Email: "testing_engine@test.com",
Password: "...",
ConfirmPassword: "..."
}),
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err))
我正在使用 axios-cookiejar-support 库。
我有一个包含 body 的 POST,但出于某种原因,Cookie 没有被注入到请求中。我这里做错了什么:
return axios
.post(
urlJoin(
config.portal.url,
'Account/Register'),
{
UserName: "testing_engine@test.com",
UserFirstName: "First Name",
UserLastName: "Last Name",
Email: "testing_engine@test.com",
Password: "...",
ConfirmPassword: "..."
},
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err))
奇怪的是,如果我对同一端点执行 GET,Cookie 就会通过:
return axios
.get(
urlJoin(
config.portal.url,
'Account/Register'),
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err));
此外,如果我执行 POST 而没有 body,他们将通过:
.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();
正如我评论的那样,我怀疑序列化部分。因为当您将数据作为查询字符串传递时,它会按预期工作。所以像这样尝试
var qs = require('qs');
return axios
.post(
urlJoin(
config.portal.url,
'Account/Register'),
qs.stringify({
UserName: "testing_engine@test.com",
UserFirstName: "First Name",
UserLastName: "Last Name",
Email: "testing_engine@test.com",
Password: "...",
ConfirmPassword: "..."
}),
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err))