3

I'm sending GET requests like this in Node JS in a loop

    request({
        url : 'https://somelink.com',
        method: 'GET'
    },
    function (error, response, body) {
        console.log(response);
    });

Since the response is async, is it possible to get the original request URL in the response?

Thanks!

user2028856
  • 3,063
  • 8
  • 44
  • 71

1 Answers1

2

You can get the original request href in the response.request object, like so:

const request = require("request");
request({
    url : 'https://google.com',
    method: 'GET'
},
function (error, response, body) {
    if (!error) {
        console.log("Original url:", response.request.uri.href);
        console.log("Original uri object:", response.request.uri);
    }
});

You can access more information in the request.uri object, for example:

console.log("Original uri:", response.request.uri);

This will give you some more useful information like port, path, host etc.

Terry Lennox
  • 29,471
  • 5
  • 28
  • 40