Sergio
Sergio

Reputation: 73

Get request object in Request module

With the Node JS request module it is possible to get the response, but, is there any way of getting the request headers sent?

Upvotes: 0

Views: 60

Answers (1)

skirtle
skirtle

Reputation: 29132

I'm not sure what the official way of doing this is but there are several things that seem to work.

If you haven't bound the callback to another this value then it will just be the request, e.g.:

request.get(options, function() {
    console.log(this.getHeader('... header name ...'));
    console.log(this.headers);
});

You could also access the request using response.request:

request.get(options, function(err, response) {
    console.log(response.request.getHeader('... header name ...'));
    console.log(response.request.headers);
});

That second approach should work anywhere that you have access to the response.

I believe these are the relevant lines in the source code:

https://github.com/request/request/blob/253c5e507ddb95dd88622087b6387655bd0ff935/request.js#L940

https://github.com/request/request/blob/253c5e507ddb95dd88622087b6387655bd0ff935/request.js#L1314

Upvotes: 2

Related Questions