Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read headers sent from my API with angular?

I have something similar to the following code on domain.com:

$http.post("http://api.domain.com/Controller/Method",
    JSON.stringify(data),
    {
        headers: {
            'Content-Type': 'application/json'
        }
    })
    .then(function (response) {
        console.log(response);
    }, function (response) {
        // something went wrong
    });
}

It works great communicating with my .NET API. response.data has all of the data my server needs to give me. However we have a new security token that we are passing to the client from the API and we are passing it in back to the client in the packet header. I know that the token is being passed back because I can read it in the packet on the network tab in chrome debugger. However response.headers() only contains content-type:"application/json; charset=utf-8" It doesn't have what is in the packet. Any one have an idea?

The data is returned from the API like so (C#) HttpContext.Current.Response.AppendHeader("session",Guid.NewGuid().ToString());

So i would expect response to have a header called session, but it does not. It is however in the packet.

like image 810
Halter Avatar asked Sep 14 '16 19:09

Halter


People also ask

How do I view HTTP response headers?

To view the request or response HTTP headers in Google Chrome, take the following steps : In Chrome, visit a URL, right click , select Inspect to open the developer tools. Select Network tab. Reload the page, select any HTTP request on the left panel, and the HTTP headers will be displayed on the right panel.

How do I read request headers?

Reading Request Headers from Servlets Reading headers is very straightforward; just call the getHeader method of the HttpServletRequest , which returns a String if the header was supplied on this request, null otherwise.

How can you read full response in angular?

You can obtain the full response using the observe property and specifying the response value. This way Angular will hand you the full HttpResponse object. Let's see this with an example. We simply import HttpClient from the @angular/common/http package and inject is as httpClient via the service constructor.


1 Answers

Use the headers variable in success and error callbacks

$http.get('/url').
  success(function(data, status, headers, config) {
    //things to do
  })
  .error(function(data, status, headers, config) {
    //things to do
  });
like image 69
Muhammed Neswine Avatar answered Oct 03 '22 06:10

Muhammed Neswine