4

I have a REST API in GO language and the front-end in Angularjs , but when I get my resource in angular my custom header don't exist.

Controller:

  Persons.query(
    function (data, headerGetter, status) {

      var headers = headerGetter();

      console.log(headers["X-Total-Count"]); //PRINT: undefined
      console.log(headers) //PRINT: {Content-Type:application/json;charset=utf-8}
      console.log(data); //PRINT: [{name:'mr x', age:'67'}, ....]

    },
    function (error) {
      console.error(error);
    });

Model:

myApp.factory("Persons", function ($resource) {
  return $resource(api_url+"/persons");
});

Response Chrome or Firefox, any client:

Access-Control-Allow-Methods:GET
Access-Control-Allow-Origin:*
Content-Length:1839
Content-Type:application/json; charset=utf-8
Date:Thu, 12 Mar 2015 21:53:54 GMT
X-Total-Count:150
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
Mr SC
  • 162
  • 2
  • 11
  • 2
    I have removed the Go tag because the header is clearly visible .. so Go has done its job. I have added the Angular tag for you .. because this is an Angular question. – Simon Whitehead Mar 12 '15 at 22:37
  • Can you display the output of `console.log(headers)` ? – alphamikevictor Mar 18 '15 at 14:38
  • What do you mean by "when I get my resource in angular, my custom header don't exist"? When do you test this? In which code? – Anthony O. Mar 18 '15 at 14:45
  • The custom headers are not even returned by the internal call to `$http`. – line-o Mar 18 '15 at 15:32
  • 1
    This seems to be a CORS issue see http://stackoverflow.com/questions/17038436/reading-response-headers-when-using-http-of-angularjs – line-o Mar 18 '15 at 15:33

2 Answers2

5

You're making a request from a different domain than the one where your API is located, operation which is called Cross-site HTTP requests ( CORS )

In order to use custom headers you need to set another one called Access-Control-Expose-Headers

If you want clients to be able to access other headers, you have to use the Access-Control-Expose-Headers header. The value of this header is a comma-delimited list of response headers you want to expose to the client.

This header lets a server whitelist headers that browsers are allowed to access. For example:

Access-Control-Expose-Headers: X-My-Custom-Header,X-Another-Custom-Header 

This allows the X-My-Custom-Header and X-Another-Custom-Header headers to be exposed to the browser.


The way I do it in .NET ( I suppose it's kinda similar in Go):

HttpContext.Current.Response.AppendHeader("Access-Control-Expose-Headers", "X-Total-Pages, X-Records");
HttpContext.Current.Response.AppendHeader("X-Total-Pages", pages.ToString());
HttpContext.Current.Response.AppendHeader("X-Records", records.ToString());

And in AngularJS I'm getting headers like this :

var headers = headers();
headers['x-total-pages']
Cosmin
  • 2,184
  • 21
  • 38
2

So this IS a CORS issue. Setting

Access-Control-Expose-Headers:"X-Total-Count"

in the server response solves your problem.

You will need to parse the response to a number:

parseInt(headers('X-Total-Count'), 10)
line-o
  • 1,885
  • 3
  • 16
  • 33