Pipeline Pipeline - 6 months ago 79
AngularJS Question

Get a custom header in angular $http from web api controller

I am trying to pass a piece of information along with my content in my HttpResponseMessage like:

string jsonFiles = JsonConvert.SerializeObject(listFiles);
HttpResponseMessage response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(jsonFiles)
};
response.Headers.Add("Key", "Value");

return response;


However in my angular call and response I cannot see the "Key" header in response.config.headers or response.headers. Any idea why?

$http.get("/api/Locker").then(function (response) {
console.log(response);
console.log(response.headers);
console.log(response.config.headers);
});


In my Startup.cs I do have:

app.UseCors(CorsOptions.AllowAll);
app.UseWebApi(config);

Answer

As per the documentation response.config returns the config used while sending the request. response.headers is a function. Try using response.headers("Key") and see if it helps.

Comments