Set HTTP header for one request


I have one particular request in my app that requires Basic authentication, so I need to set the Authorization header for that request. I read about setting HTTP request headers, but from what I can tell, it will set that header for all requests of that method. I have something like this in my code:

$ = "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==";

But I don't want every one of my post requests sending this header. Is there any way to send the header just for the one request I want? Or do I have to remove it after my request?

This question is tagged with javascript http-headers angularjs

~ Asked on 2012-08-09 04:22:13

The Best Answer is


There's a headers parameter in the config object you pass to $http for per-call headers:

$http({method: 'GET', url: '', headers: {
    'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}

Or with the shortcut method:

$http.get('', {
    headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}

The list of the valid parameters is available in the $http service documentation.

~ Answered on 2012-08-09 04:39:01


Try this, perhaps it works ;)

.factory('authInterceptor', function($location, $q, $window) {

return {
    request: function(config) {
      config.headers = config.headers || {};

      config.headers.Authorization = 'xxxx-xxxx';

      return config;

.config(function($httpProvider) {

And make sure your back end works too, try this. I'm using RESTful CodeIgniter.

class App extends REST_Controller {
    var $authorization = null;

    public function __construct()
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {

            $this->response(null, 400);    

        $this->authorization = $this->input->get_request_header('Authorization');


~ Answered on 2014-11-27 04:59:32

Most Viewed Questions: