Javascript 认证后工作在';请求';但与';节点获取';

Javascript 认证后工作在';请求';但与';节点获取';,javascript,npm,cookies,request,node-fetch,Javascript,Npm,Cookies,Request,Node Fetch,我已经编写了一个与模块一起使用的身份验证请求,但由于“请求”已被弃用,因此无法返回所需的与我尝试使用的模块一起使用的“身份验证令牌”cookie 以下是使用“请求”的工作代码 var callback1 = function(err, httpResponse, body){ console.log("Correctly prints all the cookies we want: "); console.log(httpResponse.headers["set-cook

我已经编写了一个与模块一起使用的身份验证请求,但由于“请求”已被弃用,因此无法返回所需的与我尝试使用的模块一起使用的“身份验证令牌”cookie

以下是使用“请求”的工作代码

  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_令牌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']);});
    });
我已经尝试过按照回答中的建议使用JSON.stringify编写表单数据,但没有任何帮助。

,表格:{电子邮件:myEmail,密码:myPassword}

请求中
代码版本。它是
应用程序/x-www-form-urlencoded

(见)

但是有

正文:myForm

获取
版本的代码中。而且它是多部分/表单数据 看

  • 第一句
  • 但仍然

    “内容类型”:“应用程序/x-www-form-urlencoded”

    获取
    版本中

    如果您的API端点不接受
    multipart/form数据
    ,您可能应该从
    FormData
    切换到
    URLSearchParams


    请参见

    要将
    应用程序/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,这导致节点获取写入
    多部分/FormData
    作为标题的
    内容类型

    此外,您可能需要检查授权服务器代码,以查看它是否正确发送
    set cookie

    通过以下方式进行测试:

    • 节点获取v2.6.0
    • nodejsv12.14.0

    我也尝试过使用UrlSearchParams,但不幸的是cookie仍然无法通过。然后,请从两个版本的代码中添加请求和响应头,顺便问一下-什么是
    postData
    。我没有看到它被发布。可能
    fetchOptions1.body
    大小与
    内容长度
    不匹配。你为什么要手动设置呢?我之所以手动设置,是因为我试图发送与我在开发工具中看到的完全相同的请求,一行接一行,我不确定它是否会自动设置。我已经尝试删除它,但它仍然没有返回正确的cookie。您可以显示请求和节点请求实现的console.logs吗?如下所述,这不起作用:(在我键入URLSearchParams()时有没有提示URLSearchParams()不是蓝色的?您可能正在使用旧的nodejs,其中必须首先使用
    const{URLSearchParams}=require(“url”);