我已经写了一个与request模块一起使用的身份验证请求,但是由于'request'自请求以来,我尝试使用的node-fetch模块未能返回所需的'authentication token'cookie。已弃用。
这是使用“ request”的工作代码
var callback1 = function(err, httpResponse, body){
console.log("Correctly prints all the cookies we want: ");
console.log(httpResponse.headers["set-cookie"]);
if (err){console.log("here it is!"); throw err;}
else {
//do more with response
}
};
var callback0 = function(err, httpResponse0, body){
console.log("Check that sent cookies are identical, from request:");
console.log(httpResponse0.headers["set-cookie"][0].substr(0,httpResponse0.headers["set-cookie"][0].indexOf(";")));
if (err){throw err;}
else {
var options1 = {
url: urlString1
,headers:{
'Host': hostName
,'User-Agent': myUserAgent
,'Accept': myAccept
,'Accept-Language':myAcceptLanguage
,'Accept-Encoding': myAcceptEncoding
,'Referer': "https://"+hostName+'/login'
,'Cookie': httpResponse0.headers["set-cookie"][0].substr(0,httpResponse0.headers["set-cookie"][0].indexOf(";"))
,'Content-Type': "application/x-www-form-urlencoded"
,'Content-Length': Buffer.byteLength(querystring.stringify(postData))
,'Connection': "keep-alive"
,'Upgrade-Insecure-Requests': "1"
,'DNT': "1"
,'TE': "Trailers"
}
,form: {email: myEmail, password: myPassword}
};
request.post(options1, callback1);
}
}
var options0 = {
url: urlString0
,headers:{
'Host': hostName
,'User-Agent': myUserAgent
,'Accept': myAccept
,'Accept-Language':myAcceptLanguage
,'Accept-Encoding': myAcceptEncoding
,'Referer': "https://"+hostName
,'Connection': "keep-alive"
,'Upgrade-Insecure-Requests': "1"
}
};
request(options0, callback0);
这是节点获取中的代码,无法正确返回auth_token cookie:
const fetchOptions0 = {
method: 'GET',
headers:{
'Host': hostName
,'User-Agent': myUserAgent
,'Accept': myAccept
,'Accept-Language':myAcceptLanguage
,'Accept-Encoding': myAcceptEncoding
,'Referer': "https://"+hostName
,'Connection': "keep-alive"
,'Upgrade-Insecure-Requests': "1"
}
}
fetch(urlString0, fetchOptions0)
.then(res0 => {
console.log("Check that sent cookies are identical, from fetch:");
console.log(res0.headers.raw()['set-cookie'][0].substr(0, res0.headers.raw()['set-cookie'][0].indexOf(";")));
const FormData = require('form-data');
const myForm = new FormData();
myForm.append('email', myEmail);
myForm.append('password', myPassword);
var fetchOptions1 = {
method: 'POST'
,headers:{
'Host': hostName
,'User-Agent': myUserAgent
,'Accept': myAccept
,'Accept-Language':myAcceptLanguage
,'Accept-Encoding': myAcceptEncoding
,'Referer': "https://"+hostName+'/login'
,'Cookie': res0.headers.raw()['set-cookie'][0].substr(0, res0.headers.raw()['set-cookie'][0].indexOf(";"))
,'Content-Type': "application/x-www-form-urlencoded"
,'Content-Length': Buffer.byteLength(querystring.stringify(postData))
,'Connection': "keep-alive"
,'Upgrade-Insecure-Requests': "1"
,'DNT': "1"
,'TE': "Trailers"
}
,body:myForm
};
fetch(urlString1, fetchOptions1)
.then(res1=> {console.log("Incorrect cookie, missing auth:"); console.log(res1.headers.raw()['set-cookie']);});
});
我已尝试按照this答案中的建议使用JSON.stringify编写表单数据,但这无济于事。
答案 0 :(得分:0)
有
,格式:{email:myEmail,密码:myPassword}
在request
版本的代码中。是 application/x-www-form-urlencoded
参见https://www.npmjs.com/package/request#applicationx-www-form-urlencoded-url-encoded-forms)
但是有
body:myForm
在fetch
版本的代码中。这是 multipart/form-data
见
但还是
“内容类型”:“ application / x-www-form-urlencoded”
fetch
版
如果您的API端点不接受multipart/form-data
,则您可能应该从FormData
切换到URLSearchParams
。
参见https://www.npmjs.com/package/node-fetch#post-with-form-parameters
答案 1 :(得分:0)
要将application/x-www-form-urlencoded
有效载荷发送到正文中,只需传递URLSearchParams对象
const params = new URLSearchParams();
params.append("email", myEmail);
params.append("password", myPassword);
fetch(urlString1, {
headers: {
// your headers
// omit content-length and content-type because node-fetch
// will handle that for you
},
body: params
}).then(response => {
console.log(response.headers.raw());
})
在您的示例中,您似乎正在使用FormData,这导致节点抓取将multipart/formdata
写入为标头的content-type
。
您还可能需要检查授权服务器代码,以查看其是否正确发送了set-cookie
标头。
经过测试: