【发布时间】:2016-04-09 09:24:51
【问题描述】:
我的问题是访问 Discord API(api/gateway) 时出现 301 错误。
在 Postman(REST 客户端)中,它工作正常:
但如果我使用我的代码执行此操作,它总是会返回 301 错误:
function getGateway(){
http.get({
host: 'discordapp.com',
path: '/api/gateway',
headers: {
'User-Agent': useragent //This is a variable.
}
}, function(res) {
if (res.statusCode === 200) {
var body = [];
res.setEncoding('utf8');
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function() {
body = body.join('');
return body.url;
});
} else {
if (res.statusCode === 429){
setTimeout(function() {
getGateway();
}, parseInt(JSON.parse(res.headers).Retry-After, 10) * 1000)
} else {
console.log('Received non-ok response: ' + res.statusCode);
}
}
});
}
输出:
undefined
Received non-ok response: 301
错误代码 301 甚至不在 API 文档中。
谁能看到我做错了什么?
【问题讨论】: