-1

I'm sending a request to a node server; the server responds with headers and a blank body. I can see the headers in the network activity panel in dev-tools but was unable to read them. This is my code:

 
    let uploaded = await fetch(presignedUrl, {
                        method: 'PUT',
                        body: blob
                      }).then(response => {

                        console.log('test 1 -', response.headers);

                        response.headers.forEach(item => {
                          console.log('test 2 -', item);
                        });

                        for (var pair of response.headers.entries()) {
                          console.log('test 3 -', pair[0] + ': ' + pair[1]);
                        }

                        console.log('test 4 -', response.headers.get('ETag'));

                      });

This is what it returns: https://i.stack.imgur.com/TF1Lq.png

i have tried foreach and entries , get functions of herder class too. the issue might be that i'm making a aws s3 put reuest (CORS) the upload was success full their were no error of any kind with request, it just i'm unable to read response ETag headers.

Ankit dagar
  • 107
  • 1
  • 6
  • I'm not sure what you mean by "unable to read them" because you didn't even attempt to read them, according to your code. Your output looks fine - it's a `Headers` object, as expected. Check the [docs](https://developer.mozilla.org/en-US/docs/Web/API/Headers) to see what methods you can call on it, to get certain headers for example (`get`), or to enumerate all the headers (`forEach`, `entries`). Try `console.log(response.headers.get('Content-Type'))` or `console.log(Object.fromEntries(response.headers.entries()))` for example. – CherryDT Aug 16 '20 at 20:36
  • Does this answer your question? [JS fetch, not getting headers on response](https://stackoverflow.com/questions/50570900/js-fetch-not-getting-headers-on-response) – CherryDT Aug 16 '20 at 20:40
  • i have checked the docs i know what response.headers.get('header-name'); but it just responds null, i have tried both for-each and entries. – Ankit dagar Aug 16 '20 at 20:41
  • Well for me it works (try `fetch('https://httpbin.org/status/200').then(response => { console.log(response.headers.get('Content-Type')) } );`, you'll get `text/html; charset=utf-8` as expected), and I cannot reproduce your problem because `http://example.com/process` is not a real URL and I don't know what header you are accessing. Please show a reproducible example. – CherryDT Aug 16 '20 at 20:42
  • You mention "I have tried both forEach and entries" - but you didn't show code for how you tried, so we cannot tell you what you did wrong. Please add it to your question. – CherryDT Aug 16 '20 at 20:43
  • the issue might be that i'm making a aws s3 put reuest (CORS) – Ankit dagar Aug 16 '20 at 20:45
  • But then you would see a CORS error in your devtools. Again please show a minimal reproducible example and add all the things that you tried (and exactly _how_ you tried them - code! - and _how_ they failed - errors! or expected versus actual results!), otherwise it's all just guesswork for us. I see you added "i have tried foreach and entries" to your question, but you forgot to show the code that you used and the results that you got and what you expected instead - please add it. – CherryDT Aug 16 '20 at 20:47
  • If you suspect it's related to CORS, you can play with https://www.test-cors.org/ - check the "local" tab, it then generates a URL and code according to your settings, so you can play with a server that allows/disallows CORS, returns different status, etc., and plug those URLs into your code to see what settings reproduce your problem and then show it as example. (Maybe this already tells you where the problem is.) – CherryDT Aug 16 '20 at 20:48
  • i updated the question – Ankit dagar Aug 16 '20 at 21:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219949/discussion-between-ankit-dagar-and-cherrydt). – Ankit dagar Aug 17 '20 at 09:08

1 Answers1

0

To access aws headers other then whats allowed on cors requests you need to add those herders to expose herders in expose headers in your cors config. (docs : https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html)

Ankit dagar
  • 107
  • 1
  • 6