2

In my angular app, I need to retrieve Location data from the response header. The server (Nginx) is configured to allow cross origin and to allow/expose location in header. I can see this in the chrome DEV console but in my angular app, I don't see any data for the header.

Access-Control-Allow-Origin: *
Access-Control-Allow-Headers:Location, Content-Type
Access-Control-Expose-Headers:Location, Content-Type

Here is my code snippet.

this.httpClient.post( url, {
        UserName: username,
        Password: password
      }
    )
      .subscribe(
        (res: Response) => {
          console.log(res.headers);
        },
        error => {console.log('Error occured::' + error);
        }
      );

console.log(res.headers) returns undefined.

What's going wrong here?

2 Answers 2

3

The new HttpClient returns only the body by default. Should you want to retrieve anything from the response itself you can use the option observe:

  this.httpClient.post(url, {
    UserName: username,
    Password: password
  }, {
    observe: 'response'
  })
  .subscribe(res => {
    console.log(res.headers.get('Location');
  }, error => {
    console.log('Error occured::' + error);
  });

You were typecasting res to Response so TypeScript didn’t catch your error. It’s better to remove the type as in my example so it is automatically typed correctly.

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

6 Comments

Thanks for your reply. I see only 'Content-Type' in the header. Location is missing.
Could you post all of the headers that are returned by the server for the preflight (OPTIONS) request and the actual request itself?
I see preflight response header in Network on DEV console. It returns 200. After that it returns actual response header with code 201 which has location in header. But I can't get it in Angular. I see below in console. HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ} headers Map(1) size : (...) proto : Map [[Entries]] Array(1) lazyInit null lazyUpdate : null normalizedNames Map(1) {"content-type" => "content-type"} proto
Might be missing something in Nginx server configuration ?
Yes, your CORS headers are probably incomplete. To check, could you post all of the headers that are returned by the server for the preflight (OPTIONS) request and the actual request itself? Be aware that Access-Control-Expose-Headers should only be returned by the actual request, not by the preflight.
|
0

OK. There were two issues. As @Manduro pointed out, need to add observe in the angular request. Another one is I missed the comma between two header types.

https://enable-cors.org/server_nginx.html

Need to add 'always' keyword in the location config file

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.