【问题标题】:Calling third party API gives me error: 301 Moved Permanently调用第三方 API 给我错误:301 Moved Permanently
【发布时间】:2019-07-15 08:36:11
【问题描述】:

当我使用邮递员调用 API 时,它给了我成功响应,但是我尝试使用 Node js 应用程序的代码相同,然后它给了我类似的错误。那么我是否在服务器端犯了任何错误或错误?我还尝试了邮递员生成的示例代码。但它也给出了一个错误。对于请求,我使用了 request-promiserequest 两个 npm 包

<html> <head><title>301 Moved Permanently</title></head> <body
bgcolor="white"> <center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center> </body> </html>

示例代码:

var options = { 
  method: 'PUT',
  url: 'http://IPAdd/app/v1/device/mac/zz',
  headers: 
   {
     'content-type': 'application/json',
     authorization: 'Token '+ token },
  body: 
   { //body JSON},
  json: true,"rejectUnauthorized": false 
};

request(options, function (error, response, body) {
if (error) throw new Error(error);

console.log(body);
});

【问题讨论】:

    标签: node.js api request postman


    【解决方案1】:

    我需要将followAllRedirects: true, 与请求对象一起传递以跟踪所有重定向

    var options = { 
      method: 'PUT',
      url: 'http://IPAdd/app/v1/device/mac/zz',
      headers: 
       {
         'content-type': 'application/json',
         authorization: 'Token '+ token },
      body: 
       { //body JSON},
      json: true,
      "rejectUnauthorized": false , 
      followAllRedirects: true
    };
    
    request(options, function (error, response, body) {
    if (error) throw new Error(error);
    
    console.log(body);
    });
    

    【讨论】:

      猜你喜欢
      • 2014-07-05
      • 2017-06-13
      • 2012-01-09
      • 2017-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 2020-10-07
      相关资源
      最近更新 更多