Closed Kikobeats closed 6 years ago
You can use this API:
.on('redirect', response, nextOptions) redirect event to get the response object of a redirect. The second argument is options for the next request to the redirect location.
Then save HTTP status code for each redirect. If you want more, it could store all the responses, but there's no need. It'd be too bloated.
Building upon @szmarczak's suggestion, the following code snippet produces the output you desire:
const redirects = [];
const request = got.get('https://httpbin.org/relative-redirect/5');
request.on('redirect', info => {
redirects.push([info.statusCode, info.url]);
});
const response = await request;
Thanks! Works like a charm.
Hi @brandon93s, How to add Bearer token in this call? as I am using it my my redirect but getting:
headers:
{ server: 'nginx/1.12.1',
date: 'Thu, 31 Jan 2019 15:41:50 GMT',
'content-type': 'application/json',
'content-length': '15',
connection: 'close' },
body: 'No access token' }
as I don't have issue if i use unirest
const unirest = require("unirest");
const req = unirest("POST", "https://xxxxxxx");
req.send({
"client_id": "xxxxxxxx",
"client_secret": "xxcxcxcxcxcxxcxcxcxcxcxcxcxcxcxcxcxcxc",
"audience": "https:/xcxcxcxcxcxcxcxx/",
"grant_type": "client_credentials"
});
req.end(function (res) {
if (res.error) throw new Error(res.error);
console.log("this is res.body : ", res.body);
});
access_token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsI
which returns my expected access token, the longer I can't do this, I am tempted to use unirest instead, only because rest of the team is using got therefore I am using it.
Currently
redirectUrls
expose a collection of each redirect until reach the final URL:I want to ask if could support a structure similar to that to know the HTTP status code associated with each redirect.