Я написал запрос аутентификации, который работает с модулем request , но не может вернуть желаемый 'токен аутентификации' cook ie с модулем node-fetch , который я я пытаюсь двигаться в сторону, так как «запрос» устарел.
Вот рабочий код с использованием '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);
А вот код в node-fetch, который не может правильно вернуть auth_token cook ie:
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']);});
});
Я попытался записать данные формы, используя JSON .stringify, как предложено в этом ответе, но это не помогло.