Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set custom headers with a $resource action?

Tags:

angularjs

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 ?

like image 649
François Romain Avatar asked Sep 20 '13 19:09

François Romain


People also ask

How do I send a custom header with requests?

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.

How do I add a custom header to request?

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.

What are custom headers?

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.


2 Answers

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' }     } }); 
like image 173
zs2020 Avatar answered Oct 04 '22 11:10

zs2020


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 } 
like image 34
MoonStom Avatar answered Oct 04 '22 11:10

MoonStom