Manel
Manel

Reputation: 1654

Javascript fetch api : Can't retrieve some response header

I am using the javascript fetch API to query a cross-domain api using this code :

fetch('https://api.com/search?query="2016"').then(function (response) {

  console.log(response.headers.get('Access-Control-Allow-Headers'))

  console.log(response.headers.get('Content-Range'))
  console.log(response.headers.get('Accept-Range'))

  console.log(response.headers.get('Date'))
  console.log(response.headers.get('Content-Type'))
})

The response headers are as follow :

Accept-Range:cars 300
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Content-Range, Accept-Range
Access-Control-Allow-Methods:PUT, POST, OPTIONS, GET
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Connection:keep-alive
Content-Range:0-99/1941
Content-Type:application/json
Date:Tue, 12 Jan 2016 12:17:55 GMT
Transfer-Encoding:chunked

What is very strange is that only "Content-Type" is working for others i get null :

null
null
null
null
application/json

What do i need to do to retrieve those null headers ?

Upvotes: 10

Views: 9070

Answers (2)

ofir_aghai
ofir_aghai

Reputation: 3321

using CORS package. and put this parameters:

cors({credentials: true, origin: true, exposedHeaders: '*'})

like wrote here: https://stackoverflow.com/a/64536201/2591785

Upvotes: 2

Manel
Manel

Reputation: 1654

Damn i was using 'Access-Control-Allow-Headers' instead of 'Access-Control-Expose-Headers'

It works now

Upvotes: 22

Related Questions