Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I need to add a custom header to the $http.get method, I did not understand how to do it.

var hello = _helper.server.http($http, url)
    .success(function(response){
        //do something
    });

How do I add a custom header ('X-user') and a corresponding value (say: "pqrs") ?

share|improve this question
3  
Read the documentation: docs.angularjs.org/api/ng/service/$http. Every $http method takes a config object as argument, and this config object contains headers. –  JB Nizet Jul 13 at 9:13
1  
could you post more code? You could be making a mistake in the http function in _helper.server –  akashrajkn Jul 13 at 11:00

2 Answers 2

up vote 1 down vote accepted

I am assuming _helper.server.http is a different function, I think you are making a mistake while passing the header

You have to send the header as a object

var customHeader = {'X-user': 'pqrs'}

to

var hello = _helper.server.http($http, url, customHeader)

and under the function http in _helper.server, you have to add the headers there and then call the http post method,

http: function($http, urlParam, param) {
    var request = {
        url: urlParam,
        headers: param
    };

    return $http.post(request);
}

It would help if you post more code, this could be the mistake that you are making

share|improve this answer

You need to use the config :

var config = {headers:  {
        "X-user" : "pqrs"
    }
};

$http.get(url, config).success(function(response){
   //Do something
});
share|improve this answer
    
i tried the above solution, i am still unable to set the header –  swordfish12 Jul 13 at 9:43
    
@swordfish12, please check the documentation, this is how its done, you must have done a mistake elsewhere –  akashrajkn Jul 13 at 9:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.