-2

Why does Request and Fetch return different headers?

Are the headers in Response objects always limited using Fetch? Despite it being used on server, various modes used?

Request seems to return a rich set of headers, while Fetch does not.

Other than language mechanic differences (Promise based, Callback, etc..etc..) How do these two differ? Why is one not returning a rich set of headers and the other is?

fileURL used in both examples are cross domain. On a Node/Express server, a call is made to a Google CDN.

Using Request
https://github.com/request/request

request(fileURL, (err, res, body) => {
  //headers in res
  //are pretty rich
});

Using Fetch
https://github.com/matthew-andrews/isomorphic-fetch

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

const reqConfig = { method: 'POST',
                    headers: myHeaders,
                    body: data,
                    mode: 'cors', //or 'no-cors', or this field blank..
                    cache: 'default' };

fetch(fileURL, reqConfig).then(res => {
  //headers in res
  //are not very complete
})

// I don't have access to CORS policies on server so that is not an option..

edit Even when operating in {mode: 'no-cors'} the headers are not full..

GN.
  • 8,672
  • 10
  • 61
  • 126
  • If this is for `node` then which `fetch` polyfill are you using for `node`? – Jack Oct 31 '16 at 23:20
  • 1
    Why downvote? If downvote, plz do everyone a favor and explain why. Thanks.. – GN. Nov 02 '16 at 17:49
  • I didn't downvote, but I think the question is a bit strange...if you really require all `headers` in `node` then `fetch` is almost certainly the wrong choice...since it was designed for the browser which is going to control headers for security. The only reason, that I can think of, that would make sense to use `fetch` in node is if you have universal app and the same code needs to run in both places (client+server)...but if you need all these headers then that can't be the case. So long story short, why are you using `fetch` in `node`? – Jack Nov 02 '16 at 18:22
  • Comparing functionality between libraries isn't really that strange. If `fetch` is wrong choice in this scenario, perhaps that's the answer. – GN. Nov 02 '16 at 18:58
  • I didn't say comparing functionality between libraries is strange. I said this comparison is strange for the reasons I stated, which was only to speculate on why people would downvote it. The problem with SO is that a lot of the time the questions are wrong or flawed in some way...so there's this conflict between answering the question as stated, as I tried to do (operating under the assumption the OP knows something we don't)... or try to argue for the "correct" way to something, which is often more trouble than its worth. – Jack Nov 02 '16 at 21:01
  • Appreciate the input. Answer could be there is a better tool for job. Pointing out flaws in the question (i.e. pointing out Fetch is not great for this) is always more valuable than a "downvote and run". – GN. Nov 03 '16 at 01:36

2 Answers2

1

Since you're running in cors mode the server you request from has to white list the headers you can see.

By default when doing CORS requests only these headers are available:

  • Cache-Control
  • Content-Language
  • Content-Type
  • Expires
  • Last-Modified
  • Pragma
Jack
  • 20,735
  • 11
  • 48
  • 48
0

fetch is probably not what you want for node unless you have a universal app and need the same code to run on both the client and server. fetch was created for the browser so it will -- at least in the browser -- lock down your headers (see my other answer) so if your desire is to have full access to headers then something specfically for node would be best.

Jack
  • 20,735
  • 11
  • 48
  • 48