1

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") ?

2
  • 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. Commented Jul 13, 2015 at 9:13
  • 1
    could you post more code? You could be making a mistake in the http function in _helper.server Commented Jul 13, 2015 at 11:00

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

You need to use the config :

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

$http.get(url, config).success(function(response){
   //Do something
});

2 Comments

i tried the above solution, i am still unable to set the header
@swordfish12, please check the documentation, this is how its done, you must have done a mistake elsewhere

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.