Quantcast
Channel: Active questions tagged header - Stack Overflow
Viewing all articles
Browse latest Browse all 649

Use fetch API to access headers

$
0
0

I am trying to access the headers of a POST call to a RESTful API using fetch API. How can that be done? Specifically I want to access the Set-Cookie header to be able to manage JWT tokens in all future requests after having logged in. Using Postman everything works as it should. I make the login call and then I can see that Postman has retrieved a cookie (JWT token). Now I try to do the same with fetch API.

What I did so far is accessing the API through the Url http://localhost:8081/api/auth/login using POST. The headers I added for fetch API where:

{"method":"POST","headers":    {"Accept":"application/json","Content-Type":"application/json"    }}

The fetch API looks like this:

fetch("http://localhost:8081/api/auth/login", requestInit).then((response: Response) => {    this.logger.debug("post", "Response from server for address " + fullAddress +".");    console.log("Headers: " + response.headers.getSetCookie());}).catch((error: any) => {    console.log("post", "Server error: " + error);});

In this example I would expect to access the headers, i.e. the Set-Cookie header using this response.headers.getSetCookie(). The string is empty though.


Viewing all articles
Browse latest Browse all 649

Trending Articles