Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Header Location value from a Fetch request in browser

From a ReactJS - Redux front app, I try to get the Location Header value of an REST API response.

When I Curl this :

curl -i -X POST -H "Authorization: Bearer MYTOKEN" https://url.company.com/api/v1.0/tasks/

I Have this answer :

HTTP/1.1 202 ACCEPTED
Server: nginx
Date: Fri, 12 Aug 2016 15:55:47 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Location: https://url.company.com/api/v1.0/tasks/status/idstatus

When I make a Fetch in ReactJS

    var url = 'https://url.company.com/api/v1.0/tasks/'
    fetch(url, {
            method: 'post',
            credentials: 'omit',
                headers: {
                    'Authorization': `Bearer ${token}`
                }
            }
     )

I don't have any Headers in the response object : No header in Fetch request

I tried all the response.headers functions I've found in https://developer.mozilla.org/en-US/docs/Web/API/Headers :

response.headers.get('Location');

But well, as headers is empty, I have empty results.

Do you know why I can't get a proper Header object filled with the headers values ?

like image 679
Luc Vanwaelscappel Avatar asked Aug 12 '16 22:08

Luc Vanwaelscappel


1 Answers

Thanks to John, I've found the answer.

I just had to put

Access-Control-Expose-Headers: Location

To my response headers and it worked, so now I can access Location value with :

response.headers.get('Location');

Thx John !

like image 197
Luc Vanwaelscappel Avatar answered Oct 08 '22 20:10

Luc Vanwaelscappel