javascript - nodejs request-promise url location not showing in headers - Stack Overflow

I'm currently using request-promise in nodejs to make a request to a website and then return the h

I'm currently using request-promise in nodejs to make a request to a website and then return the headers, as I'm trying to get the url (location) of the request incase of redirects. Although the issue I'm having is that the location is not showing up in the headers when I'm logging them after the request.

My code:

const rp = require('request-promise');

const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36';
const url = //url

var _include_headers = function(body, response, resolveWithFullResponse) {
    return {'headers': response.headers, 'data': body};
  };

const options = {
    uri: url,
    followAllRedirects: true,
    method: 'get',
    gzip: true,
    transform: _include_headers,
    headers: {
        'User-Agent': userAgent
    },
};

const p1 = rp(options).then((response, error, html) => {

    console.log(response.headers);

})

The console logs the headers, but it doesn't show the location in the headers object. Is there anyway I can find the location of the url after making the request and returning the headers?

I'm currently using request-promise in nodejs to make a request to a website and then return the headers, as I'm trying to get the url (location) of the request incase of redirects. Although the issue I'm having is that the location is not showing up in the headers when I'm logging them after the request.

My code:

const rp = require('request-promise');

const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36';
const url = //url

var _include_headers = function(body, response, resolveWithFullResponse) {
    return {'headers': response.headers, 'data': body};
  };

const options = {
    uri: url,
    followAllRedirects: true,
    method: 'get',
    gzip: true,
    transform: _include_headers,
    headers: {
        'User-Agent': userAgent
    },
};

const p1 = rp(options).then((response, error, html) => {

    console.log(response.headers);

})

The console logs the headers, but it doesn't show the location in the headers object. Is there anyway I can find the location of the url after making the request and returning the headers?

Share Improve this question asked Dec 16, 2017 at 4:45 adevhadevh 1134 silver badges12 bronze badges 1
  • You're passing followAllRedirects: true and the default for followRedirect is true. That means that you're telling the request() library to HIDE the redirects from you by just following them and giving you only the final result after redirect. If you want to get the redirect status and URL yourself, then you have to tell the request() library NOT to follow the redirects automatically so it will return the 3xx status and headers to you. – jfriend00 Commented Dec 16, 2017 at 7:24
Add a ment  | 

2 Answers 2

Reset to default 3

What is your request-promise version? You can try this snippet.

var rp = require('request-promise');

    rp({
        uri: 'http://google.',
        method: 'GET',
        resolveWithFullResponse: true
    })
        .then(function (response) {
            console.dir(response.headers);
        });

When you set followAllRedirects to true, you won't have a location field in your response headers, and the only way to access the final redirected URL is to access the response.request.uri object that contains the final URL in its href property.

To do so, you can modify your transform function to pass an additional finalUrl property that is equal to response.request.uri.href:

const rp = require('request-promise');

const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36';
const url = "http://www.stackoverflow.";

const _include_headers = function(body, response, resolveWithFullResponse) {
    return {
        'headers': response.headers, 
        'data': body, 
        'finalUrl': response.request.uri.href // contains final URL
    };
};

const options = {
    uri: url,
    followAllRedirects: true,
    method: 'get',
    gzip: true,
    transform: _include_headers,
    headers: {
        'User-Agent': userAgent
    },
};

const p1 = rp(options).then((response, error, html) => {

    console.log(response.finalUrl);
});

In the example above, http://www.stackoverflow. is redirected to https://stackoverflow., which is what the program prints.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745282201a4620342.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信