Categories
Angular Answers

How to read response headers from API response with Angular and TypeScript?

Spread the love

Sometimes, we want to read response headers from API response with Angular and TypeScript.

In this article, we’ll look at how to read response headers from API response with Angular and TypeScript.

How to read response headers from API response with Angular and TypeScript?

To read response headers from API response with Angular and TypeScript, we can set the observer option to 'response'.

For instance, we write

http.get<any>(url, { observe: "response" }).subscribe((resp) => {
  console.log(resp.headers.get("X-Token"));
});

in a method to make a GET request with http.get.

We call it with the url to make the request to and an object with the observer property set to 'response' to get the response.

In the subscribe callback, we get the response header by the key with resp.headers.get.

Conclusion

To read response headers from API response with Angular and TypeScript, we can set the observer option to 'response'.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *