HttpClient
Performs HTTP requests.
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>
}
See also
Description
HttpClient
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
).
Methods
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 overloads...Show All Hide All expand_moreOverload #1Sends an
Parameters
Returns
Overload #2Constructs a request that interprets the body as an
Parameters
Returns
Overload #3Constructs a request that interprets the body as a blob and returns the response as a blob.
Parameters
Returns
Overload #4Constructs a request that interprets the body as a text string and returns a string value.
Parameters
Returns
Overload #5Constructs a request that interprets the body as an
Parameters
Returns
Overload #6Constructs a request that interprets the body as a
Parameters
Returns
Overload #7Constructs a request which interprets the body as a text string and returns the full event stream.
Parameters
Returns
Overload #8Constructs a request which interprets the body as a JSON object and returns the full event stream.
Parameters
Returns
Overload #9Constructs a request which interprets the body as a JSON object and returns the full event stream.
Parameters
Returns
Overload #10Constructs a request which interprets the body as an
Parameters
Returns
Overload #11Constructs a request which interprets the body as a
Parameters
Returns
Overload #12Constructs a request which interprets the body as a text stream and returns the full
Parameters
Returns
Overload #13Constructs a request which interprets the body as a JSON object and returns the full
Parameters
Returns
Overload #14Constructs a request which interprets the body as a JSON object and returns
the full
Parameters
Returns
Overload #15Constructs a request which interprets the body as a JSON object and returns the full
Parameters
Returns
Overload #16Constructs a request which interprets the body as a JSON object with the response body of the requested type.
Parameters
Returns
Overload #17Constructs a request where response type and requested observable are not known statically.
Parameters
Returns
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 overloads...Show All Hide All expand_moreOverload #1Constructs a
Parameters
Returns
Overload #2
Parameters
Returns
Overload #3Constructs a
Parameters
Returns
Overload #4Constructs a
Parameters
Returns
Overload #5Constructs a
Parameters
Returns
Overload #6Constructs a
Parameters
Returns
Overload #7Constructs a
Parameters
Returns
Overload #8Constructs a
Parameters
Returns
Overload #9Constructs a
Parameters
Returns
Overload #10Constructs a
Parameters
Returns
Overload #11Constructs a
Parameters
Returns
Overload #12Constructs a
Parameters
Returns
Overload #13Constructs a
Parameters
Returns
Overload #14Constructs a
Parameters
Returns
Overload #15Constructs a DELETE request that interprets the body as a JSON object and returns the response in a given type.
Parameters
Returns
|
Constructs an observable that, when subscribed, causes the configured
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 overloads...Show All Hide All expand_moreOverload #1Constructs a
Parameters
Returns
Overload #2
Parameters
Returns
Overload #3Constructs a
Parameters
Returns
Overload #4Constructs a
Parameters
Returns
Overload #5Constructs a
Parameters
Returns
Overload #6Constructs a
Parameters
Returns
Overload #7Constructs a
Parameters
Returns
Overload #8Constructs a
Parameters
Returns
Overload #9Constructs a
Parameters
Returns
Overload #10Constructs a
Parameters
Returns
Overload #11Constructs a
Parameters
Returns
Overload #12Constructs a
Parameters
Returns
Overload #13Constructs a
Parameters
Returns
Overload #14Constructs a
Parameters
Returns
Overload #15Constructs a
Parameters
Returns
|
Constructs an observable that, when subscribed, causes the configured
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 overloads...Show All Hide All expand_moreOverload #1Constructs a
Parameters
Returns
Overload #2
Parameters
Returns
Overload #3Constructs a
Parameters
Returns
Overload #4Constructs a
Parameters
Returns
Overload #5Constructs a
Parameters
Returns
Overload #6Constructs a
Parameters
Returns
Overload #7Constructs a
Parameters
Returns
Overload #8Constructs a
Parameters
Returns
Overload #9Constructs a
Parameters
Returns
Overload #10Constructs a
Parameters
Returns
Overload #11Constructs a
Parameters
Returns
Overload #12Constructs a
Parameters
Returns
Overload #13Constructs a
Parameters
Returns
Overload #14Constructs a
Parameters
Returns
Overload #15Constructs a
Parameters
Returns
|
Constructs an |
||||||
Constructs a
Parameters
Returns
|
||||||
Constructs a
Parameters
Returns
|
Constructs an |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 overloads...Show All Hide All expand_moreOverload #1Constructs an
Parameters
Returns
Overload #2Constructs an
Parameters
Returns
Overload #3Constructs an
Parameters
Returns
Overload #4Constructs an
Parameters
Returns
Overload #5Constructs an
Parameters
Returns
Overload #6Constructs an
Parameters
Returns
Overload #7Constructs an
Parameters
Returns
Overload #8Constructs an
Parameters
Returns
Overload #9Constructs an
Parameters
Returns
Overload #10Constructs an
Parameters
Returns
Overload #11Constructs an
Parameters
Returns
Overload #12Constructs an
Parameters
Returns
Overload #13Constructs an
Parameters
Returns
Overload #14Constructs an
Parameters
Returns
Overload #15Constructs an
Parameters
Returns
|
Constructs an observable that, when subscribed, causes the configured
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 overloads...Show All Hide All expand_moreOverload #1Constructs a
Parameters
Returns
Overload #2
Parameters
Returns
Overload #3Constructs a
Parameters
Returns
Overload #4Constructs a
Parameters
Returns
Overload #5Constructs a
Parameters
Returns
Overload #6Constructs a
Parameters
Returns
Overload #7Constructs a
Parameters
Returns
Overload #8Constructs a
Parameters
Returns
Overload #9Constructs a
Parameters
Returns
Overload #10Constructs a
Parameters
Returns
Overload #11Constructs a
Parameters
Returns
Overload #12Constructs a
Parameters
Returns
Overload #13Constructs a
Parameters
Returns
Overload #14Constructs a
Parameters
Returns
Overload #15Constructs a
Parameters
Returns
|
Constructs an observable that, when subscribed, causes the configured
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 overloads...Show All Hide All expand_moreOverload #1Constructs a
Parameters
Returns
Overload #2
Parameters
Returns
Overload #3Constructs a
Parameters
Returns
Overload #4Constructs a
Parameters
Returns
Overload #5Constructs a
Parameters
Returns
Overload #6Constructs a
Parameters
Returns
Overload #7Constructs a POST request that interprets the body as a JSON object and returns the full event stream.
Parameters
Returns
Overload #8Constructs a POST request that interprets the body as a JSON object and returns the full event stream.
Parameters
Returns
Overload #9Constructs a POST request that interprets the body as an
Parameters
Returns
Overload #10Constructs a
Parameters
Returns
Overload #11Constructs a
Parameters
Returns
Overload #12Constructs a
Parameters
Returns
Overload #13Constructs a
Parameters
Returns
Overload #14Constructs a
Parameters
Returns
Overload #15Constructs a
Parameters
Returns
|
Constructs an observable that, when subscribed, causes the configured
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 overloads...Show All Hide All expand_moreOverload #1Constructs a
Parameters
Returns
Overload #2
Parameters
Returns
Overload #3Constructs a
Parameters
Returns
Overload #4Constructs a
Parameters
Returns
Overload #5Constructs a
Parameters
Returns
Overload #6Constructs a
Parameters
Returns
Overload #7Constructs a
Parameters
Returns
Overload #8Constructs a
Parameters
Returns
Overload #9Constructs a
Parameters
Returns
Overload #10Constructs a
Parameters
Returns
Overload #11Constructs a
Parameters
Returns
Overload #12Constructs a
Parameters
Returns
Overload #13Constructs a
Parameters
Returns
Overload #14Constructs a
Parameters
Returns
Overload #15Constructs a
Parameters
Returns
|
Usage notes
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')));
}