Modify HttpHeaders in post


#1

I am trying to send a post Http request to my server, but need to add additional headers to it. If I just post like this:

let headers = new HttpHeaders();
this.http.post(url, req, { headers: headers });

I see the following request options in my post request (ionic serve in chrome):

:authority:my server
:method:POST
:path:path on my server
:scheme:https
accept:application/json, text/plain, */*
accept-encoding:gzip, deflate, br
accept-language:en-GB,en-US;q=0.9,en;q=0.8
content-length:90
content-type:text/plain
origin:http://localhost:8100
referer:http://localhost:8100/
user-agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36

However, if I try add custom http headers in any way (set, append, json object in constructor), e.g.:

let headers = new HttpHeaders().set('Content-Type', 'application/json');
this.http.post(url, req, { headers: headers })

The post request seems to be changed to an OPTIONS request, and the name of the header I am trying to set appears as the value for access-control-request-headers (as well as some other weird changes):

:authority:my server
:method:OPTIONS
:path:path on my server
:scheme:https
accept:*/*
accept-encoding:gzip, deflate, br
accept-language:en-GB,en-US;q=0.9,en;q=0.8
access-control-request-headers:content-type
access-control-request-method:POST
origin:http://localhost:8100
user-agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36

The same happens if I use Firefox.

Am I doing something wrong?