我正在使用 axios-cookiejar-support图书馆。
我有一个包含正文的 POST,但出于某种原因,Cookie 没有被注入(inject)到请求中。我在这里做错了什么:
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,它们将通过:
.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))
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();
最佳答案
正如我评论的那样,我怀疑序列化部分。因为当您将数据作为查询字符串传递时,它会按预期工作。所以像这样尝试
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))
关于javascript - 正文未传递 Cookie 的 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41142299/