HttpClient
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[]; }; observe?: HttpObserve; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "arraybuffer" | ... 2 more ... | "json"; withCredentials?: boolean; } = {}): Observable<any>
delete(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: HttpObserve; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; } = {}): Observable<any>
get(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: HttpObserve; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; } = {}): Observable<any>
head(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: HttpObserve; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; } = {}): Observable<any>
jsonp<T>(url: string, callbackParam: string): Observable<T>
options(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: HttpObserve; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; } = {}): Observable<any>
patch(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: HttpObserve; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; } = {}): Observable<any>
post(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: HttpObserve; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; } = {}): Observable<any>
put(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: HttpObserve; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; } = {}): Observable<any>
}
参见
说明
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.
方法
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 #1Sends an
参数
返回值
Overload #2Constructs a request that interprets the body as an
参数
返回值
Overload #3Constructs a request that interprets the body as a blob and returns the response as a blob.
参数
返回值
Overload #4Constructs a request that interprets the body as a text string and returns a string value.
参数
返回值
Overload #5Constructs a request that interprets the body as an
参数
返回值
Overload #6Constructs a request that interprets the body as a
参数
返回值
Overload #7Constructs a request which interprets the body as a text string and returns the full event stream.
参数
返回值
Overload #8Constructs a request which interprets the body as a JSON object and returns the full event stream.
参数
返回值
Overload #9Constructs a request which interprets the body as a JSON object and returns the full event stream.
参数
返回值
Overload #10Constructs a request which interprets the body as an
参数
返回值
Overload #11Constructs a request which interprets the body as a
参数
返回值
Overload #12Constructs a request which interprets the body as a text stream and returns the full
参数
返回值
Overload #13Constructs a request which interprets the body as a JSON object and returns the full
参数
返回值
Overload #14Constructs a request which interprets the body as a JSON object and returns the full
参数
返回值
Overload #15Constructs a request which interprets the body as a JSON object and returns the full
参数
返回值
Overload #16Constructs a request which interprets the body as a JSON object with the response body of the requested type.
参数
返回值
Overload #17Constructs a request where response type and requested observable are not known statically.
参数
返回值
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
You can pass an 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
|
Constructs an observable that, when subscribed, causes the configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1Constructs a
参数
返回值
Overload #2Constructs a
参数
返回值
Overload #3Constructs a
参数
返回值
Overload #4Constructs a
参数
返回值
Overload #5Constructs a
参数
返回值
Overload #6Constructs a
参数
返回值
Overload #7Constructs a
参数
返回值
Overload #8Constructs a
参数
返回值
Overload #9Constructs a
参数
返回值
Overload #10Constructs a
参数
返回值
Overload #11Constructs a
参数
返回值
Overload #12Constructs a
参数
返回值
Overload #13Constructs a
参数
返回值
Overload #14Constructs a
参数
返回值
Overload #15Constructs a DELETE request that interprets the body as a JSON object and returns the response in a given type.
参数
返回值
|
Constructs an observable that, when subscribed, causes the configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1Constructs a
参数
返回值
Overload #2Constructs a
参数
返回值
Overload #3Constructs a
参数
返回值
Overload #4Constructs a
参数
返回值
Overload #5Constructs a
参数
返回值
Overload #6Constructs a
参数
返回值
Overload #7Constructs a
参数
返回值
Overload #8Constructs a
参数
返回值
Overload #9Constructs a
参数
返回值
Overload #10Constructs a
参数
返回值
Overload #11Constructs a
参数
返回值
Overload #12Constructs a
参数
返回值
Overload #13Constructs a
参数
返回值
Overload #14Constructs a
参数
返回值
Overload #15Constructs a
参数
返回值
|
Constructs an observable that, when subscribed, causes the configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1Constructs a
参数
返回值
Overload #2Constructs a
参数
返回值
Overload #3Constructs a
参数
返回值
Overload #4Constructs a
参数
返回值
Overload #5Constructs a
参数
返回值
Overload #6Constructs a
参数
返回值
Overload #7Constructs a
参数
返回值
Overload #8Constructs a
参数
返回值
Overload #9Constructs a
参数
返回值
Overload #10Constructs a
参数
返回值
Overload #11Constructs a
参数
返回值
Overload #12Constructs a
参数
返回值
Overload #13Constructs a
参数
返回值
Overload #14Constructs a
参数
返回值
Overload #15Constructs a
参数
返回值
|
Constructs an | ||||||
Constructs a
参数
返回值
| ||||||
Constructs a
参数
返回值
|
Constructs an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1Constructs an
参数
返回值
Overload #2Constructs an
参数
返回值
Overload #3Constructs an
参数
返回值
Overload #4Constructs an
参数
返回值
Overload #5Constructs an
参数
返回值
Overload #6Constructs an
参数
返回值
Overload #7Constructs an
参数
返回值
Overload #8Constructs an
参数
返回值
Overload #9Constructs an
参数
返回值
Overload #10Constructs an
参数
返回值
Overload #11Constructs an
参数
返回值
Overload #12Constructs an
参数
返回值
Overload #13Constructs an
参数
返回值
Overload #14Constructs an
参数
返回值
Overload #15Constructs an
参数
返回值
|
Constructs an observable that, when subscribed, causes the configured | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1Constructs a
参数
返回值
Overload #2Constructs a
参数
返回值
Overload #3Constructs a
参数
返回值
Overload #4Constructs a
参数
返回值
Overload #5Constructs a
参数
返回值
Overload #6Constructs a
参数
返回值
Overload #7Constructs a
参数
返回值
Overload #8Constructs a
参数
返回值
Overload #9Constructs a
参数
返回值
Overload #10Constructs a
参数
返回值
Overload #11Constructs a
参数
返回值
Overload #12Constructs a
参数
返回值
Overload #13Constructs a
参数
返回值
Overload #14Constructs a
参数
返回值
Overload #15Constructs a
参数
返回值
|
Constructs an observable that, when subscribed, causes the configured | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1Constructs a
参数
返回值
Overload #2Constructs a
参数
返回值
Overload #3Constructs a
参数
返回值
Overload #4Constructs a
参数
返回值
Overload #5Constructs a
参数
返回值
Overload #6Constructs a
参数
返回值
Overload #7Constructs a POST request that interprets the body as a JSON object and returns the full event stream.
参数
返回值
Overload #8Constructs a POST request that interprets the body as a JSON object and returns the full event stream.
参数
返回值
Overload #9Constructs a POST request that interprets the body as an
参数
返回值
Overload #10Constructs a
参数
返回值
Overload #11Constructs a
参数
返回值
Overload #12Constructs a
参数
返回值
Overload #13Constructs a
参数
返回值
Overload #14Constructs a
参数
返回值
Overload #15Constructs a
参数
返回值
|
Constructs an observable that, when subscribed, causes the configured | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 个重载形式...显示所有 隐藏所有 expand_moreOverload #1Constructs a
参数
返回值
Overload #2Constructs a
参数
返回值
Overload #3Constructs a
参数
返回值
Overload #4Constructs a
参数
返回值
Overload #5Constructs a
参数
返回值
Overload #6Constructs a
参数
返回值
Overload #7Constructs a
参数
返回值
Overload #8Constructs a
参数
返回值
Overload #9Constructs a
参数
返回值
Overload #10Constructs a
参数
返回值
Overload #11Constructs a
参数
返回值
Overload #12Constructs a
参数
返回值
Overload #13Constructs a
参数
返回值
Overload #14Constructs a
参数
返回值
Overload #15Constructs a
参数
返回值
|
使用说明
Sample HTTP requests for the Tour of Heroes application.
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});
}
JSONP Example
requestJsonp(url, callback = 'callback') {
return this.httpClient.jsonp(this.heroesURL, callback);
}
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')));
}