尝试在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();
答案 0 :(得分:1)
请求path
中的options
不正确。前置/
并瞧!
简单地改变:
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();