I'm unable to change the headers when doing a POST request. I tried a couple of things:
Simple class:
export class HttpService {
constructor(http: Http) {
this._http = http;
}
}
I tried:
testCall() {
let body = JSON.stringify(
{ "username": "test", "password": "abc123" }
)
let headers = new Headers();
headers.append('Content-Type', 'application/json'); // also tried other types to test if its working with other types, but no luck
this._http.post('http://mybackend.local/api/auth', body, {
headers: headers
})
.subscribe(
data => { console.log(data); },
err => { console.log(err); },
{} => { console.log('complete'); }
);
}
2:
testCall() {
let body = JSON.stringify(
{ "username": "test", "password": "abc123" }
)
let headers = new Headers();
headers.append('Content-Type', 'application/json'); // also tried other types to test if its working with other types, but no luck
let options = new RequestOptions({
headers: headers
});
this._http.post('http://mybackend.local/api/auth', body, options)
.subscribe(
data => { console.log(data); },
err => { console.log(err); },
{} => { console.log('complete'); }
);
}
none of the two are working. I didn't forget to import any of the classes.
I'm using Google Chrome. So I check the 'Network' tab, my request is there, and it says my Content-Type is text/plain.
Is this a bug or am I doing something wrong?
UPDATE
I forgot to import the Headers class from Angular2/http:
import {Headers} from 'angular2/http';
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…