我正在使用 let request = require('request');
request.post("https://some-server.com/asyncUserLogin", {
form: {
username: 'rapidtest',
password: 'rapidtest123',
TARGET_URL: targetUrl
},
jar: jar1
}, (err, res, body) => {
/*
* I am trying to get cookies from this request
* and pass it to the next in multiple ways,
* here I am stripping cookie manually,
* and trying to set Cookie: header
*/
let cookies = jar1.getCookies("https://some-server.com/asyncUserLogin");
let cookiesString: string = "";
cookies.forEach(cookie => {
if(cookiesString.length > 0)
cookiesString += ";"
cookiesString += cookie.key + "=" + cookie.value;
});
/*
* in this request I am trying to send POST to:
* https://some-server.com/getOauthCookie?grant_type=password_assertion&client_id=xxx-yyyy
* along with cookies retrieved in previous request,
* it should respone with 200 and some other cookies
*/
request.post("https://some-server.com/getOauthCookie", {
har: {
postData: {
mimeType: 'application/x-www-form-urlencoded',
params: [
{
name: 'grant_type',
value: 'password_assertion'
},
{
name: 'client_id',
value: 'xxx-yyyy'
}
]
},
headers: [
{
name: 'Content-Type',
value: 'application/x-www-form-urlencoded'
},
{
name: 'Cookies',
value: cookiesString
}
],
}
jar: jar1
}, (err, res, body) => {
//this request fails
});
});
库向服务器发出http请求(https://github.com/request/request)。
Unfornatelly我得到400回复,因为我的请求中可能缺少一些数据(可能是cookie)。
我就是这样做的:
ads
我应该怎么做,确保第一次请求的cookie被传递给第二个请求?
如何显示我正在做的请求,如果缺少某些内容/格式错误(How to view request sent from node.js to server?)?
答案 0 :(得分:0)
您可以访问完整回复正文中的完整请求 - response.request
,然后从请求标题中提取您的Cookie值
request(options, function (error, response, body) {
if (error) throw new Error(error);
// This will give you complete request
console.log(response.request);
//This gives you the headers from Request
console.log(response.request.headers['Cookies']);
});