HttpClient
执行 HTTP 请求。该服务作为可注入类提供,带有执行 HTTP 请求的方法。每个请求方法都有多个签名,并且返回类型会根据所调用的签名(主要的值是 observe
和 responseType
)而有所不同。
Performs HTTP requests. This service is available as an injectable class, with methods to perform HTTP requests. Each request method has multiple signatures, and the return type varies based on the signature that is called (mainly the values of observe
and responseType
).
class HttpClient {
request(first: string | HttpRequest<any>, url?: string, options: { body?: any; headers?: HttpHeaders | { [header: string]: string | string[]; }; context?: HttpContext; observe?: "body" | "events" | "response"; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<...>; }; reportProgress?: boolean; responseType?: "arraybuffer" | ... 2 more ... | "json"... = {}): Observable<any>
delete(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; context?: HttpContext; observe?: "body" | "events" | "response"; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | ... 1 more ... | boolean>; }; reportProgress?: boolean; responseType?: "arraybuffer" | ... 2... = {}): Observable<any>
get(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; context?: HttpContext; observe?: "body" | "events" | "response"; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | ... 1 more ... | boolean>; }; reportProgress?: boolean; responseType?: "arraybuffer" | ... 2... = {}): Observable<any>
head(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; context?: HttpContext; observe?: "body" | "events" | "response"; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | ... 1 more ... | boolean>; }; reportProgress?: boolean; responseType?: "arraybuffer" | ... 2... = {}): Observable<any>
jsonp<T>(url: string, callbackParam: string): Observable<T>
options(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; context?: HttpContext; observe?: "body" | "events" | "response"; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | ... 1 more ... | boolean>; }; reportProgress?: boolean; responseType?: "arraybuffer" | ... 2... = {}): Observable<any>
patch(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; context?: HttpContext; observe?: "body" | "events" | "response"; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | ... 1 more ... | boolean>; }; reportProgress?: boolean; responseType?: "arraybuffer" | ... 2... = {}): Observable<any>
post(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; context?: HttpContext; observe?: "body" | "events" | "response"; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | ... 1 more ... | boolean>; }; reportProgress?: boolean; responseType?: "arraybuffer" | ... 2... = {}): Observable<any>
put(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; context?: HttpContext; observe?: "body" | "events" | "response"; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | ... 1 more ... | boolean>; }; reportProgress?: boolean; responseType?: "arraybuffer" | ... 2... = {}): Observable<any>
}
参见
Provided in
说明
请注意, responseType
选项的值是一个字符串,用于标识此响应的单一数据类型。该方法的各个重载版本处理每种响应类型。正如组合签名所暗示的那样 responseType
的值不能为联合类型。
Note that the responseType
options value is a String that identifies the single data type of the response. A single overload version of the method handles each response type. The value of responseType
cannot be a union, as the combined signature could imply.
Further information available in the Usage Notes...
方法
为任意 HTTP 请求构造一个可观察的对象,该请求在被订阅时将通过已注册的拦截器链触发该请求,然后发送到服务器。 Constructs an observable for a generic HTTP request that, when subscribed, fires the request through the chain of registered interceptors and on to the server. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
17 个重载形式...显示所有 隐藏所有 expand_moreOverload #1发送 Sends an
参数
返回值响应对象的
Overload #2构造一个请求,它将请求体解释为 Constructs a request that interprets the body as an
参数
返回值响应对象的
Overload #3构造一个请求,将请求体解释为 Constructs a request that interprets the body as a blob and returns the response as a blob.
参数
返回值此请求的响应对象的
Overload #4构造一个请求,它将请求体解释为字符串,并且返回一个字符串。 Constructs a request that interprets the body as a text string and returns a string value.
参数
返回值此请求的响应对象的
Overload #5构造一个请求,它将请求体解释为 Constructs a request that interprets the body as an
参数
返回值一个响应对象的
Overload #6构造一个请求,它将请求体解释为 Constructs a request that interprets the body as a
参数
返回值此请求的
Overload #7构造一个请求,它将请求体解释为文本字符串,并且返回完整的事件流。 Constructs a request which interprets the body as a text string and returns the full event stream.
参数
返回值此请求的
Overload #8构造一个请求,它将请求体解释为 JSON 对象,并且返回完整的 HTTP 事件流。 Constructs a request which interprets the body as a JSON object and returns the full event stream.
参数
返回值此请求的
Overload #9构造一个请求,它将请求体解释为 JSON 对象,并且返回完整的 HTTP 事件流。 Constructs a request which interprets the body as a JSON object and returns the full event stream.
参数
返回值此请求的
Overload #10构造一个请求,它将请求体解释为 Constructs a request which interprets the body as an
参数
返回值此请求的响应对象的
Overload #11构造一个请求,它将请求体解释为 Constructs a request which interprets the body as a
参数
返回值此请求的响应对象的
Overload #12构造一个请求,它将请求体解释为文本流,并且返回完整的 Constructs a request which interprets the body as a text stream and returns the full
参数
返回值此请求的
Overload #13构造一个请求,它将请求体解释为 JSON 对象,并返回完整 Constructs a request which interprets the body as a JSON object and returns the full
参数
返回值此请求的完整
Overload #14构造一个请求,它将请求体解释为 JSON 对象,并返回带有请求主体类型 Constructs a request which interprets the body as a JSON object and returns the full
参数
返回值此请求的完整
Overload #15构造一个请求,它将请求体解释为 JSON 对象,并返回完整的 Constructs a request which interprets the body as a JSON object and returns the full
参数
返回值此请求的响应对象的
Overload #16构造一个请求,它将请求体解释为 JSON 对象,并返回所请求类型的响应体。 Constructs a request which interprets the body as a JSON object with the response body of the requested type.
参数
返回值此请求的响应对象的
Overload #17构造一个请求,其中的响应类型和所请求的可观察对象都不是静态已知的。 Constructs a request where response type and requested observable are not known statically.
参数
返回值所请求的响应对象的
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
你可以直接把 You can pass an 或者,你可以将 HTTP 方法作为第一个参数,将 URL 字符串作为第二个参数,将包含请求正文的选项哈希作为第三个参数。参见 Alternatively you can pass an HTTP method as the first parameter, a URL string as the second, and an options hash containing the request body as the third. See
The
|
构造一个 Observable,当它被订阅时,会要求服务器执行配置好的 Constructs an observable that, when subscribed, causes the configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1构造一个 Constructs a
参数
返回值类型为
Overload #2构造一个 Constructs a
参数
返回值类型为
Overload #3构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #4构造一个 Constructs a
参数
返回值此请求的所有
Overload #5构造一个 Constructs a
参数
返回值此请求的所有
Overload #6构造一个 Constructs a
参数
返回值表示啥此请求的
Overload #7构造一个 Constructs a
参数
返回值表示啥此请求的
Overload #8构造一个 Constructs a
参数
返回值表示此请求的
Overload #9构造一个 Constructs a
参数
返回值此请求的完整
Overload #10构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #11构造一个 Constructs a
参数
返回值此请求的完整
Overload #12构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #13构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #14构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #15构造一个 Constructs a DELETE request that interprets the body as a JSON object and returns the response in a given type.
参数
返回值此请求的响应对象的
|
构造一个 Observable,当它被订阅时,会要求服务器执行配置好的 Constructs an observable that, when subscribed, causes the configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1构造一个 Constructs a
参数
返回值响应对象的
Overload #2构造一个 Constructs a
参数
返回值响应对象的
Overload #3构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #4构造一个 Constructs a
参数
返回值表示此请求的
Overload #5构造一个 Constructs a
参数
返回值响应对象的
Overload #6构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #7构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #8构造一个 Constructs a
参数
返回值响应对象的
Overload #9构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #10构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #11构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #12构造一个 Constructs a
参数
返回值此请求的完整
Overload #13构造一个 Constructs a
参数
返回值此请求的完整
Overload #14构造一个 Constructs a
参数
返回值响应体的
Overload #15构造一个 Constructs a
参数
返回值此请求的响应对象的
|
构造一个 Observable,当它被订阅时,会要求服务器执行配置好的 Constructs an observable that, when subscribed, causes the configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1构造一个 Constructs a
参数
返回值响应对象的
Overload #2构造一个 Constructs a
参数
返回值响应对象的
Overload #3构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #4构造一个 Constructs a
参数
返回值此请求的
Overload #5构造一个 Constructs a
参数
返回值此请求的
Overload #6构造一个 Constructs a
参数
返回值表示啥此请求的
Overload #7构造一个 Constructs a
参数
返回值表示啥此请求的
Overload #8构造一个 Constructs a
参数
返回值表示此请求的
Overload #9构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #10构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #11构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #12构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #13构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #14构造一个 Constructs a
参数
返回值响应对象的
Overload #15构造一个 Constructs a
参数
返回值此请求的响应对象的
|
构造一个 Constructs an | ||||||
为给定的 URL 和回调参数的名称构造一个 Constructs a
参数
返回值响应对象的
| ||||||
为给定的 URL 和回调参数的名称构造一个 Constructs a
参数
返回值响应对象的
|
构造一个 Constructs an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1构造一个 Constructs an
参数
返回值响应对象的
Overload #2构造一个 Constructs an
参数
返回值响应对象的
Overload #3构造一个 Constructs an
参数
返回值此请求的响应对象的
Overload #4构造一个 Constructs an
参数
返回值此请求的
Overload #5构造一个 Constructs an
参数
返回值此请求的
Overload #6构造一个 Constructs an
参数
返回值此请求的
Overload #7构造一个 Constructs an
参数
返回值表示啥此请求的
Overload #8构造一个 Constructs an
参数
返回值此请求的
Overload #9构造一个 Constructs an
参数
返回值此请求的响应对象的
Overload #10构造一个 Constructs an
参数
返回值此请求的响应对象的
Overload #11构造一个 Constructs an
参数
返回值此请求的响应对象的
Overload #12构造一个 Constructs an
参数
返回值此请求的响应对象的
Overload #13构造一个 Constructs an
参数
返回值此请求的响应对象的
Overload #14构造一个 Constructs an
参数
返回值响应对象的
Overload #15构造一个 Constructs an
参数
返回值此请求的响应对象的
|
构造一个 Observable,当它被订阅时,会要求服务器执行配置好的 Constructs an observable that, when subscribed, causes the configured | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1构造一个 Constructs a
参数
返回值响应对象的
Overload #2构造一个 Constructs a
参数
返回值响应对象的
Overload #3构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #4构造一个 Constructs a
参数
返回值此请求的
Overload #5构造一个 Constructs a
参数
返回值表示此请求的
Overload #6构造一个 Constructs a
参数
返回值表示啥此请求的
Overload #7构造一个 Constructs a
参数
返回值此请求的
Overload #8构造一个 Constructs a
参数
返回值此请求的
Overload #9构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #10构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #11构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #12构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #13构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #14构造一个 Constructs a
参数
返回值响应对象的
Overload #15构造一个 Constructs a
参数
返回值此请求的响应对象的
|
构造一个 Constructs an observable that, when subscribed, causes the configured | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1构造一个 Constructs a
参数
返回值响应对象的
Overload #2构造一个 Constructs a
参数
返回值响应对象的
Overload #3构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #4构造一个 Constructs a
参数
返回值此请求的
Overload #5构造一个 Constructs a
参数
返回值此请求的
Overload #6构造一个 Constructs a
参数
返回值此请求的
Overload #7构造一个 Constructs a POST request that interprets the body as a JSON object and returns the full event stream.
参数
返回值此请求的
Overload #8构造一个 Constructs a POST request that interprets the body as a JSON object and returns the full event stream.
参数
返回值此请求的
Overload #9构造一个 POST 请求,它将请求体解释为 Constructs a POST request that interprets the body as an
参数
返回值此请求的响应对象的
Overload #10构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #11构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #12构造一个 Constructs a
参数
返回值表示啥此请求的
Overload #13构造一个 Constructs a
参数
返回值表示此请求的
Overload #14构造一个 Constructs a
参数
返回值响应对象的
Overload #15构造一个 Constructs a
参数
返回值表示此请求的
|
构造一个 Constructs an observable that, when subscribed, causes the configured | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1构造一个 Constructs a
参数
返回值响应对象的
Overload #2构造一个 Constructs a
参数
返回值响应对象的
Overload #3构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #4构造一个 Constructs a
参数
返回值此请求的
Overload #5构造一个 Constructs a
参数
返回值此请求的
Overload #6构造一个 Constructs a
参数
返回值表示啥此请求的
Overload #7构造一个 Constructs a
参数
返回值此请求的
Overload #8构造一个 Constructs a
参数
返回值此请求的
Overload #9构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #10构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #11构造一个 Constructs a
参数
返回值表示啥此请求的
Overload #12构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #13构造一个 Constructs a
参数
返回值此请求的响应对象的
Overload #14构造一个 Constructs a
参数
返回值JSON 格式的响应对象的
Overload #15构造一个 Constructs a
参数
返回值所请求类型的
|
使用说明
“英雄之旅”应用程序的示例 HTTP 请求。
Sample HTTP requests for the Tour of Heroes application.
HTTP 请求示例
HTTP Request Example
// GET heroes whose name contains search term
searchHeroes(term: string): observable<Hero[]>{
const params = new HttpParams({fromString: 'name=term'});
return this.httpClient.request('GET', this.heroesUrl, {responseType:'json', params});
}
Alternatively, the parameter string can be used without invoking HttpParams by directly joining to the URL.
this.httpClient.request('GET', this.heroesUrl + '?' + 'name=term', {responseType:'json'});
JSONP 示例
JSONP Example
requestJsonp(url, callback = 'callback') {
return this.httpClient.jsonp(this.heroesURL, callback);
}
PATCH 示例
PATCH Example
// PATCH one of the heroes' name
patchHero (id: number, heroName: string): Observable<{}> {
const url = `${this.heroesUrl}/${id}`; // PATCH api/heroes/42
return this.httpClient.patch(url, {name: heroName}, httpOptions)
.pipe(catchError(this.handleError('patchHero')));
}