Если вы проверите параметры метода post()
модуля HttpClient
, вы сможете увидеть приемлемые параметры, как показано ниже:
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* Construct a POST request which interprets the body as JSON and returns the full response.
*
* @return an `Observable` of the `HttpResponse` for the request, with a body type of `Object`.
*/
post(url: string, body: any | null, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe: 'response';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<HttpResponse<Object>>;
, что означает, что функция принимает url
, body
иoptions
как основные приемлемые параметры.Вы можете указать пользовательские заголовки, создав функцию, которая будет принимать имя и значение заголовка.как в случае с одним пользовательским заголовком, в случае нескольких заголовков, вы можете написать свою собственную версию функции.
private setHeaders(customHeader: string, value: string): HttpHeaders {
const headers: HttpHeaders = new HttpHeaders({
customHeader: value,
});
return headers;
}
и в последнее время не забудьте импортировать эти библиотеки.
import { HttpClient, HttpHeaders } from '@angular/common/http';