My problem is I get a 301 error when accessing the Discord API(api/gateway).
In Postman(REST Client), it works fine:
But if I do it with my code, it always returns a 301 error:
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);
}
}
});
}
Output:
undefined
Received non-ok response: 301
Error code 301 is not even in the API documentation.
Can somebody see what I did wrong?