with $http, we can do this:
var config = { headers: { 'something': 'anything' } }; $http.get('url/to/json', config) .success(function() { // do something… })
i would like to do the same with a $resource reference (not working):
var config = { headers: { 'something': 'anything' } }; MyResource.get( config, function() { // success // do something… } );
with the corresponding service declared like this :
.factory('MyResource', function($resource){ return $resource('url/to/json'); })
it's not working : the config object goes to the url and not in the http headers.
Is there a way to do that ?
Custom Headers are for troubleshooting, informational purposes, and specific server-side logic. For example, to send a GET request with a custom header name, you can use the "X-Real-IP" header, which defines the client's IP address. For a load balancer service, "client" is the last remote host.
In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.
Custom headers allow site owners to upload their own “title” image to their site, which can be placed at the top of certain pages. These can be customized and cropped by the user through a visual editor in the Appearance > Header section of the admin panel. You may also place text beneath or on top of the header.
headers
for $resource
is available since AngularJS 1.1.1. Make sure you have correct version used.
The format is
$resource('url/to/json', {}, {headers: { 'something': 'anything' }});
[edit by zuma] The above doesn't seem right. The third parameter to $resource should be a different. This seems more correct to me:
$resource('url/to/json', {}, { get: { method: 'GET', headers: { 'something': 'anything' } } });
The headers
object inside a resource action supports both static
values for its fields, but also dynamic
values returned from a function.
$resource('url/to/json', {}, { get: { method: 'GET', headers: { 'header_static': 'static_value', 'header_dynamic': dynamicHeaderVal } } }); function dynamicHeaderVal(requestConfig){ // this function will be called every time the "get" action gets called // the result will be used as value for the header item // if it doesn't return a value, the key will not be present in the header }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With