nodejs imgur api - 超过容量 500 错误

nodejs imgur api - over capacity 500 error

我在 nodejs 中尝试使用 imgur 搜索 api 时不断收到此错误:

{"data":{"error":"Imgur is temporarily over capacity. Please try again later."},"success":false,"status":500}

我在通过 REST 客户端访问 api 时没有收到此错误,这让我觉得我做错了什么。当我使用 REST 客户端时,我得到了我期望的结果。这就是我在节点中访问 api 的方式。

var https = require("https");

var options = {
  protocol:"https:",
  host:"api.imgur.com",
  path:"3/gallery/search/top/all/0?q=cats",
  headers:{"Authorization" : "Client-ID ***********"}
}

var req = https.request(options, function(response) {
  var str = '';
  response.on('data', function (chunk) {
    str += chunk;
  });
  response.on('end', function () {
    console.log(str);
  });
});

req.end();  

您在请求 options 中的 path 不正确。前置 / 瞧!

只需更改:

path:"3/gallery/search/top/all/0?q=cats",

path:"/3/gallery/search/top/all/0?q=cats",

以下内容非常适合我!

var https = require("https");

var options = {
  protocol:"https:",
  host:"api.imgur.com",
  path:"/3/gallery/search/top/all/0?q=cats",
  headers:{"Authorization" : "Client-ID {YOUR_ID}"}
}

var req = https.request(options, function(response) {
  var str = '';
  response.on('data', function (chunk) {
    str += chunk;
  });
  response.on('end', function () {
    console.log(JSON.parse(str));
  });
});

req.end();