node-fetch / node-fetch

A light-weight module that brings the Fetch API to Node.js
MIT License
8.76k stars 1.03k forks source link

View final request headers? #787

Open travis-w opened 4 years ago

travis-w commented 4 years ago

Is there a good way to view the final request headers that are sent in the fetch request?

I am working on a library that uses node-fetch to send http requests and I was trying to set up some http level logging, so it would be nice to log out what exact headers are sent in the request. I am able to log the user set headers, however node-fetch generates the default headers if they don't exist so I would like to log those as well.

Is there a good way to get these? Or do I basically need to recreate the getNodeRequestOptions for my own use if I want to keep using node-fetch? Or maybe it would be possible to get this function exported at the module level for util use?

Thanks!

xxczaki commented 4 years ago

I can't think of any way to access all the headers sent, but here you can find the default node-fetch headers (and when they are set).

Hope this helps.

travis-w commented 4 years ago

Thanks for the response. The only issue with just having those, is that content-length is computed, so I would have to calculate it myself in order to log that. And then there is a chance, unless I basically just strip out certain parts of node-fetch into my own code, that the headers I calculate, are still different than the ones node-fetch sends.

For anyone that has a similar need, I ended up switching to sindresorhus/got. It has a hook, that allows you to inspect the request right before it is sent.

FDiskas commented 5 months ago

As a reference:

NODE_DEBUG=http