当前位置 : 首页 » 互动问答 » 正文

Set HTTP header for one request

分类 : 互动问答 | 发布时间 : 2012-08-09 12:22:13 | 评论 : 2 | 浏览 : 286490 | 喜欢 : 158

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:

$http.defaults.headers.post.Authorization = "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?

回答(2)

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

    $http({method: 'GET', url: 'www.google.com/someapi', headers: {
        'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
    });
    

    Or with the shortcut method:

    $http.get('www.google.com/someapi', {
        headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
    });
    

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

  • 2楼
  • 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) {
      $httpProvider.interceptors.push('authInterceptor');
    })
    

    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()
        {
            parent::__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'] ) {
                die();
            }
    
            if(!$this->input->get_request_header('Authorization')){
                $this->response(null, 400);    
            }
    
            $this->authorization = $this->input->get_request_header('Authorization');
        }
    
    }
    

相关阅读:

How to move an element into another element?

Set HTTP header for one request

Set HTTP header for one request

Set HTTP header for one request

How to set focus on input field?

Remove duplicates from an array of objects in JavaScript

Angular ng-click with call to a controller function not working

Can I call jquery click() to follow an <a> link if I haven't bound an event handler to it with bind or click already?

Check whether a string matches a regex in JS

jQuery event to trigger action when a div is made visible