3

I am doing a call to a REST endpoint. I want to add a resource (below). However, when my service calls Http's post method it will invoke the request but the response's header(s) are not returned. At least, I experience an empty headers object of the Response instance. (??) I do expect response header. In particular I expect the Location header as part of a "REST ADD RESOURCE" pattern. The Location headers contains the URL of the newly created resource.

The weird thing about this: when I call my API directly (thus not via Angular2), I get the exact response but this time with (all) expected headers including the Location response header.

Uhh, is there something wrong with Http.post or am I doing something wrong?

Mind you: My service returns an Observable Response in this example. This is not the intended class type to return to the caller. I am using it for the convenience of understanding what is happening with the post response. In reality it is my intention to pass the url stored in the Location header.

addModel(model: any): Observable<Response> {
let token = this.loginService.auth.accessToken;
let headers = new Headers({
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': `Bearer ${token}`
});
let options = new RequestOptions({headers: headers});
let data: string = JSON.stringify(model);
return this.http.post(this.url, data, options)

  // .map(r => {
  //   console.log("Response ", r);
  //   return r.headers.get("Location"); // THERE IS NOTHING IN headers (?). Don't understand.
  // })
  .catch(err => Observable.throw(err));
 }
3
  • If its a CORS request the server needs to add Allow-access-expose-headers otherwise the returned headers won't be available to JS. Did you check in the browsers devtools if the response contains the headers? Commented Nov 9, 2016 at 13:31
  • Hi Gunter, I had logged to console but did not see any header. I also used Postman to manually fire the request and I could see the headers in Postman. (But not in http.post of Angular2). I am going to checkout the CORS header you suggested. Most probably I also need to do some adjustment in the WSO2's API Manager. Commented Nov 9, 2016 at 13:39
  • Logging to the console doesn't help because it needs to be read using JS first which doesn't work (as mentioned). If it works from postman, than it's definitely a CORS issue. Commented Nov 9, 2016 at 13:40

1 Answer 1

7

With a CORS request the server needs to add Allow-access-expose-headers: headername otherwise the returned headers won't be available to JS.

You can investigate the request response in the browsers devtools (network tab AFARK) if the response actually contains the headers?

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

7 Comments

Hi Gunter, I just checked how my CORS settings are on the API Gateway for this particular rest api. First of all, CORS is on. Currently some headers are configured to be allowed for the CORS request. (I added also Location , since it was not there ..(but I think this won't be necessary because the Location header belongs to a server response and not the request). Nothing changed. Btw, I dont have Allow-access-expose-headers but Access-Control-Allow-Headers. Is this an issue?
Yes it is an issue. ...expose-headers is about exposing ;-) Did you investigate the reponse in the browsers devtools? You'll find the response there but JS won't expose it.
Hi Gunter, yes I checked in DevTools and it turned out that the remote server (API) did send all expected headers under the condition of enabled CORS. I could see the Location header on the response in Chrome Devtools. So, all headers (Location) have reached my browser, but Angular's http instance does not pass them through, so I can't access them in my code. At the moment I am trying to configure WSO2 API Manager to include the CORS header that you suggested. Until now I was not successful in including it because I still does not see it in my response. I 'll do a post on the stackoverflow.
Gunter, I was successful in configuring my API Gateway (WSO2) to include the suggested header (Access-Control-Expose-Headers) in the response. I did a test and Angular2's Http client picks up my Location header (visible). Your suggestion helped a lot!.Thanks.
|

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.