Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Headers added in AngularJS only show on Access-Control-Request-Headers

I am trying to use an interceptor to add a custom header to every request in an AngularJS App using the following code:

angular.module('app').factory('httpRequestInterceptor', function () {
    return {
        request: function (config) {
            config.headers['testheader'] = 'testheaderworks';

            return config;
        }
    };
});

angular.module('app').config(function ($httpProvider) {    
    $httpProvider.interceptors.push('httpRequestInterceptor');
});

This code was copied from the answer to this question

Unfortunately, when I examine the resulting requests, I get the following:

Provisional headers are shown

Access-Control-Request-Headers:accept, testheader

Access-Control-Request-Method:GET

Origin:http://localhost:61577

Referer:http://localhost:61577/

User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36

I confirmed this in both the network tab in Chrome and on the server side. Why is the custom header key 'testheader' added to Access-Control-Request-Headers rather than the general headers? What happened to the value? Is there another way to add custom headers to every AngularJS request that avoids this issue?

like image 695
Random Person Avatar asked Nov 20 '15 17:11

Random Person


People also ask

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

Do I need access control allow headers?

The Access-Control-Allow-Headers response header is used in response to a preflight request which includes the Access-Control-Request-Headers to indicate which HTTP headers can be used during the actual request. This header is required if the request has an Access-Control-Request-Headers header.

What is HTTP request custom header?

Custom HTTP headers can be used to filter requests or specify a value for the Accept header. Some endpoints employ custom HTTP headers to filter data returned by a GET or POST request.

How do I see request headers?

To view the request or response HTTP headers in Google Chrome, take the following steps : In Chrome, visit a URL, right click , select Inspect to open the developer tools. Select Network tab. Reload the page, select any HTTP request on the left panel, and the HTTP headers will be displayed on the right panel.


1 Answers

In case anyone reads this and is having the same issue:

The problem was that Angular was making a cross origin request, which the browser was preventing. In order to enable this request I had to enable the header on the server side. In our case (NodeJs) the code to do this was:

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Headers", testheader");
    next();
});
like image 187
Random Person Avatar answered Sep 30 '22 13:09

Random Person