Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Read Custom HTTP Headers Sent by the Server in Angular

    Juri StrumpflohnerJuri Strumpflohner

    By default the response body doesn’t contain all the data that might be needed in your app. Your server might return some special header which you have to read explicitly. In such case we can use the { observe: ‘response’} configuration of the Angular HttpClient. Let’s explore how.

    angularAngular
    4 - 6
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    When we execute an HTTP request to our backend, you can see how we get back to data. In this case, we even specify Tom typing here, so we want to get back an instance of a person which we can then access here in our subscribe callback in our app component for instance.

    What if we are interested in actually some custom headers which our response gives us back? For instance, in this example here, in the response headers here after we request, you can see there is the my custom header. I would like to read that header out.

    In order to do so, I have to change basically the call here because the person itself only contains the raw data coming from the response body. I'm interested also in the header values in this case. To do so, we can specify here so-called observe value.

    The observe value specifies what we are currently interested in in observing and therefore also changes here the response type of our HTTP request. We could have here different kind of values, for instance, events when you are interested in HTTP events. We would get here an observable of an HTTP event.

    In our case, we are interested in the response specifically. According to here we get now an HTTP response of type person. We need to import that from the common HTTP package. We can now go to our app component here. Let's Console log what comes back from data.

    If I execute request, you can see now we have a series of properties here which is our response object actually that gets printed here out on our UI. Now, to retrieve our custom header, what we can do here is to access the header property. Then we use the get method and specify here my custom header which is the header name I specified on the server side.

    Again, executing here the request and then looking here in response headers, I can see that my custom header gets back again. Here also now on the Console, we get Angular rocks printed out which is actually the value of that my custom header.

    Discuss

    Discuss