Я вызываю REST API с помощью HttpClient в Angular 8 с приведенным ниже кодом, но даже если код состояния равен 200, тело всегда равно нулю. Почему?
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
}),
'observe': 'response'
};
var resp = new HttpResponse(this.http.post(this.apiURL + "/token/", {username: "test", password: "test"}, httpOptions));
Редактировать:
Поскольку мне нужен доступ к коду состояния, я изменил функцию и создал службу.
getToken(username, password): HttpClient<any>{
return this.http.post(this.http.post(this.apiURL + "/token/", {username: username, password: password}, {
headers: new HttpHeaders({'Content-Type': 'application/json'}),
observe: 'response'
});
Интересно, что параметры параметров должны быть встроены, если я использую переменную httpOptions, я получаю ошибку:
TS2345: Argument of type '{ headers: HttpHeaders; observe: string; }'
is not assignable to parameter of type '{ headers?: HttpHeaders | {
[header: string]: string | string[]; }; observe?: "body"; params?:
HttpParams | { [param: string]: string | string[]; }; reportProgress?:
boolean; responseType?: "json"; withCredentials?: boolean; }'.
[ng] Types of property 'observe' are incompatible.
[ng] Type 'string' is not assignable to type '"body"'.
}