Axios不会在302重定向上传递cookie
我正在尝试在node.js中实现管理请求提交类。 有两个API调用,一个是登录,第二个是请求访问(两者都是表单)。 我设法在Postman中构建了这两个API呼叫,但是,我正在努力在Node.js中获得相同的行为。可能是因为饼干未正确传递。
登录流量是: 将发布请求发送到 https:// {{base_uri}} /hc/web/siteadminlogin/siteadmin/adminaccess.jsp 使用表单数据和内容类型:应用程序/x-www-form-urlenCoded
此调用的响应是302重定向,该响应设置了这两个cookie:
set> set-cookie:wshumanclickwebsession = 816D619EDDD19EE46AC03BF88B381;路径=/hc/web;安全的; httponly
set-cookie:wshumanclickserver = z1-e-e-dnv6969;路径=/hc/web;安全
然后重定向触发get请求到以下URL: https:// {{base_uri}}/hc/web/siteadmin/siteadmin/siteadmin/adminaccess.jsp?site= {{form_data_site}}}
与最初的邮寄响应中的cookie一起。
Postman会自动处理Cookie存储,但是,在Axios中,这些Cookie未正确传递给302重定向。导致登录失败并返回登录页面。
这就是我在节点中所拥有的:
const qs = require('qs');
const data = qs.stringify({
'user_name': 'user_name',
'password': 'user_pass!!',
'site_number': 'site_num',
'But1':'Submit'
});
const config = {
method: 'post',
url: 'https://base_uri/hc/web/siteadminlogin/siteadmin/AdminAccess.jsp',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data : data,
withCredentials: true,
};
try {
const response = await axios(config);
console.log(response.headers);
console.log(response.data);
}catch (e) {
console.log(e)
}
它是表单数据未正确提交,或者cookie在请求之间没有存储并传递。 关于如何解决这个问题的想法?
I'm trying to implement an admin request submission class in node.js.
There are two API calls, one is Login and the second is Request Access (Both are forms).
I've managed to build these two API calls in Postman, however, I'm struggling with getting the same behavior in node.js. Probably because cookies are not passed correctly.
The login flow is:
Send POST request to https://{{base_uri}}/hc/web/siteadminlogin/siteadmin/AdminAccess.jsp
With form data and Content-Type: application/x-www-form-urlencoded
The response from this call is 302 redirect that sets these two cookies:
Set-Cookie: WSHumanClickWebSession=816d619eddd19ee46ac03bf88b381; path=/hc/web; secure; HttpOnly
Set-Cookie: WSHumanClickServer=z1-e-dnv6969; path=/hc/web; secure
Then the redirect triggers a GET request to the following URL:
https://{{base_uri}}/hc/web/siteadmin/siteadmin/AdminAccess.jsp?site={{form_data_site}}
With the cookies from the initial POST response.
Postman handles cookie storage automatically, however, in Axios these cookies are not passed correctly to the 302 redirect. Causing the login to fail and return back to the login page.
This is what I have in node:
const qs = require('qs');
const data = qs.stringify({
'user_name': 'user_name',
'password': 'user_pass!!',
'site_number': 'site_num',
'But1':'Submit'
});
const config = {
method: 'post',
url: 'https://base_uri/hc/web/siteadminlogin/siteadmin/AdminAccess.jsp',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data : data,
withCredentials: true,
};
try {
const response = await axios(config);
console.log(response.headers);
console.log(response.data);
}catch (e) {
console.log(e)
}
It's either the form data is not being submitted correctly or the cookies are not being stored and passed between the requests.
Any ideas on how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论