I am struggling to add multiple headers to an angular http POST request. Adding a single header works fine, but anything beyond that results in bad requests. for example
Update(url): Observable<string> {
let _headers = new Headers();
_headers.append( 'accept','application/json;odata=verbose')
let options = new RequestOptions({
withCredentials: true,
headers: _headers
})
let body = '"{ hey ... this is some data }"'
return this.http.post(url, body, options)
.map(data => { return data.json() })
}
the code above generates the request properly. see below (from Fiddler)
POST http://foo.wingtip.com/pwa/_api/contextinfo/ HTTP/1.1
Host: foo.wingtip.com
Connection: keep-alive
Content-Length: 31
accept: application/json;odata=verbose
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36
content-type: text/plain
Referer: http://localhost:4200/resPlans
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: Ribbon.Tabs.TeamBuilder.Home=1920974|-1|125
"{ hey ... this is some data }"
If i add another header using .append, the generated request is wrong.. and my previous headers are gone as well..for example...
update(url): Observable<string> {
let _headers = new Headers();
_headers.append( 'accept','application/json;odata=verbose')
_headers.append('Content-Length','31') /////this breaks it
let options = new RequestOptions({
withCredentials: true,
headers: _headers
})
let body = '"{ hey ... this is some data }"'
return this.http.post(url, body, options)
.map(data => { return data.json()})
}
the resulting request (in Fiddler) looks like so
OPTIONS http://foo.wingtip.com/pwa/_api/contextinfo/ HTTP/1.1
Host: foo.wingtip.com
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://localhost:4200/resPlans
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
note that neither of the two custom headers are present.
and yes... i did import the Http, Header, and RequestOptions from Angular's Http library.
Guidance much appreciated