12

I'm requesting an endpoint that creates a new resource and returns a 201 response containing a "Location" header with the newly created resource:

Network inspecting on Chrome

However, when I try to get header value as described in Angular guide, I get a "null" value instead of the actual value. Here is my code (note: I'm setting responseType:'text' to avoid Json parsing error as the response body is empty):

(...)
import { HttpClient } from '@angular/common/http';

(...)

  constructor(public navCtrl: NavController, public navParams: NavParams, public http: HttpClient) {
  }

  ionViewDidLoad() {
    let obj = {nome : "SomeName"}
    this.http.post("http://localhost:8080/categorias", obj, {observe: 'response', responseType: 'text'})
    .subscribe(
        (resp) => {
          console.log(resp.headers.get('Location'));
        }
      );
  }

I even tried console.log(resp.headers) in order to check this object, and it shows a completely different structure:

enter image description here

How can I get a custom header from a HttpResponse object from the new HttpClient Angular API?

6
  • What is the type of http? HttpClient? Commented Dec 14, 2017 at 13:58
  • Yes. HttpClient. Commented Dec 14, 2017 at 14:02
  • Cant you set a break point and see what is contained in resp.headers? Or maybe do a console.log to see if the the headers are actually set Commented Dec 14, 2017 at 14:04
  • thought or setting interceptor, using which you can send response header of your wish? Commented Dec 14, 2017 at 14:09
  • @Jota.Toledo I tried that. I've edited the question with that information. Commented Dec 14, 2017 at 14:11

3 Answers 3

19

I encountered a similar issue with the eTag header : this is a Cross Origin issue.

From what I remember, CORS return only a couple of simple headers, such as Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, etc.

If you want to return a specific header, you have to add another header, Access-Control-Expose-Headers, containing a list of the headers you want to return with it. So, in your case, Access-Control-Expose-Headers = 'location'.

You also obvioulsy need to change your backend to return the same header to Angular.

Hope this helps !

Sign up to request clarification or add additional context in comments.

Comments

5

I had that problem too. In my case I saw only Content-Type, Pragma and Expiration headers.

In fact, that is not a Angular issue. The reason that there are not headers that you expect is a CORS.

Adding 'access-control-expose-headers' : 'Location' on the API solves your problem :)

1 Comment

The image in the question shows that the location is being supplied by the API
2
        Response.Headers.Append("myCaptchaName", "value");


services.AddCors(options =>
        {
            options.AddPolicy("AllowMyOrigin",
                builder => builder.WithOrigins("http://localhost:4200").AllowAnyHeader().
                AllowAnyMethod().AllowCredentials()
                .WithExposedHeaders("myCaptchaName"));
        });
        services.Configure<MvcOptions>(options =>
        {
            options.Filters.Add(new CorsAuthorizationFilterFactory("AllowMyOrigin"));
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.