2

I'm using angular and angular-resource version 1.1.5 and I'm using a $resource to make a request to a REST service. But it seems like the custom headers is not appended to the request. My definition is as below. Is there anything I did wrong?

myApp.factory('User', function($resource) {
    var User = $resource('http://localhost\\:7017/mydomain/users/jack', { }, {
        get: {
            method: 'GET',
            isArray: false,
            headers: {'X-Requested-By':'abc'}
        }
    });
    return User;
});
user2961105
  • 23
  • 1
  • 1
  • 4

3 Answers3

1

Read this to see how to configure default headers in one place: http://docs.angularjs.org/api/ng.$http

EDIT:

Your header must be included in Access-Control-Allow-Headers header in response to the OPTIONS request, which is sent automatically prior to your GET request.

Adam
  • 2,897
  • 2
  • 23
  • 23
  • Thanks for your reply. I'm using $resource not $http. ":" is a reserved character in $resource and I need to use double back slash. – user2961105 Nov 07 '13 at 19:12
  • Of course, you're right about the colon. Note however that $resource uses $http internally, so setting headers for $http will work for you. If not, then check this out: http://stackoverflow.com/questions/12791667/how-to-specify-headers-parameter-for-custom-angular-resource-action – Adam Nov 07 '13 at 19:28
  • Yes. the "query" is a typo. It should be "get". – user2961105 Nov 07 '13 at 19:47
  • Thanks. I have read that thread. It says 1.1.3 support $resource headers. I tried both 1.1.3 and 1.1.5 but cannot get the custom header working. – user2961105 Nov 07 '13 at 19:51
  • How do you check if the header is sent? Look in the browser console. Maybe it's a server-side issue? BTW, I got it working even with Angular 1.0.8. – Adam Nov 07 '13 at 20:01
  • I checked in Chrome Debugging tool. Under Network I can see the request headers sent to REST server. The custom header is not added in. – user2961105 Nov 07 '13 at 20:18
  • Provide example (e.g. jsfiddle or plunker) showing this behavior. – Adam Nov 07 '13 at 22:13
  • I see my custom header 'X-Requested-By' is set to Access-Control-Request-Headers. Not directly in my Request Headers. – user2961105 Nov 11 '13 at 15:24
  • Aha! So it's an OPTIONS request, right? Do you see your header in `Access-Control-Allow-Headers` response header too? If not, then your server refuses to handle it, or is just unaware. Do you have CORS configured properly on both client and server side? If not, read about [enabling CORS in Angular](http://better-inter.net/enabling-cors-in-angular-js/) and [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) in general. – Adam Nov 11 '13 at 20:35
  • Thank you, Adam. Now I set Access-Control-Allow-Headers on my server. Then the request can set X-Requested-By header correctly. – user2961105 Nov 11 '13 at 20:45
1

You can modify the default headers inside the $httpProvider.

the headers are an object and separated intocommon, patch, post and put

headers in dev tools

so if you want to change the default for all your requests, just do a

$httpProvider.defaults.headers.put['Content-Type'] = 'application/json';
tmaximini
  • 8,403
  • 6
  • 47
  • 69
1

You have to call get method by using its name, i.e User.get(callback)

It seems that custom headers do not get sent when get method is called with User.query(callback)

Shivox
  • 13
  • 1
  • 4