Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete headers from Angular.js $http request

I want to delete some $http request header fields from one specific request (it means not on the $httpProvider level). These fields are:

  • Cache-Control
  • If-Modified-Since
  • Referer
  • X-Requested-With

How to do this in a single request? I tried to use transformRequest parameter, but didn't find enough information to make it work. Such a [CoffeeScript] code:

$scope.logout = ->
  $http({
    method: 'GET'
    url: '/api/logout'
    headers: { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' }
    transformRequest: (data, headersGetter) ->
      console.log data
      console.log headersGetter
      data
  }).success ->
    $location.path('editor')

shows that data is undefined, headersGetter is function (c){a||(a=Nb(b));return c?a[y(c)]||null:a} (which says to me absolutely nothing), and I didn't understand what to return from the transformRequest function.

like image 562
Paul Avatar asked Mar 20 '13 16:03

Paul


People also ask

How do I remove HTTP request header?

Guidelines. The dp:remove-http-request-header element removes a specific header field and its associated value from the protocol header of a client request. When the client request contains the header field identified by the field attribute, removes this header field from the client request.

How do you modify the $HTTP request default Behaviour?

To add or overwrite these defaults, simply add or remove a property from these configuration objects. To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. $httpProvider. defaults.

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.

What is http headers in angular?

HTTP Headers let the client and the server share additional information about the HTTP request or response. For example, we use the content-type header to indicate the media type of the resource like JSON, text, blob, etc.


1 Answers

  1. If you use the unminified version of Angular, you'll get nicer backtraces when an exception happens, and you'll have an easier time introspecting the angular code. I personally recommend it while developing. Here's what headersGetter actually looks like:

    function (name) {
        if (!headersObj) headersObj =  parseHeaders(headers);
    
        if (name) {
          return headersObj[lowercase(name)] || null;
        }
    
        return headersObj;
      } 
    

    The data argument to your transformer will be undefined unless you’re POSTing some data.

  2. The headersGetter function takes an optional argument name, if you want to get a single header, but you omit the argument to set a header:

    headersGetter()['Cache-Control'] = 'no-cache';
    headersGetter()['X-Requested-With'] = '';
    

    The return value from your transformer should be the value of data you want to use.

  3. You can’t change the Referer header from XHR.

like image 161
Josh Lee Avatar answered Sep 30 '22 12:09

Josh Lee