Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular $http not sending header in OPTIONS pre-flight

I'm trying to connect to an API with the $http service that requires a certain header to be defined for POST requests:

app.run(['$http', function($http) {
    $http.defaults.headers.post.hello = 'world';
}]);

However, my Angular app sends an OPTIONS request before the POST, and it does not appear to be sending the custom-defined header.

Check out the network tab in this Plunker: http://plnkr.co/edit/tcqIb2q9zIQb3nf2SzyT

And watch see that where the request is supposed to send a hello header with the value of world, it actually isn't:

enter image description here

I see the hello in the access-control-request-header, but shouldn't it be on its own line? And why doesn't it send the value world?

like image 725
Bryce Avatar asked Apr 06 '26 20:04

Bryce


1 Answers

When you add a header, because you are going across different domains, requests first send an HTTP request by the OPTIONS method to the resource on the other domain, in order to determine whether the actual request is safe to send. In this request the header name is sent in the access-control-request-headers to check if it is a safe header to send.

If it is safe to send then the header will be sent with the POST.

Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests

like image 198
Wayne Ellery Avatar answered Apr 08 '26 14:04

Wayne Ellery



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!