5 lines
97 KiB
JSON
5 lines
97 KiB
JSON
{
|
||
"id": "guide/http",
|
||
"title": "Communicating with backend services using HTTP",
|
||
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/http.md?message=docs%3A%20describe%20your%20change...\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n</div>\n\n\n<div class=\"content\">\n <h1 id=\"communicating-with-backend-services-using-http\">Communicating with backend services using HTTP<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#communicating-with-backend-services-using-http\"><i class=\"material-icons\">link</i></a></h1>\n<p>Most front-end applications need to communicate with a server over the HTTP protocol, in order to download or upload data and access other back-end services.\nAngular provides a simplified client HTTP API for Angular applications, the <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> service class in <code>@angular/common/<a href=\"api/common/http\" class=\"code-anchor\">http</a></code>.</p>\n<p>The HTTP client service offers the following major features.</p>\n<ul>\n<li>The ability to request <a href=\"guide/http#typed-response\">typed response objects</a>.</li>\n<li>Streamlined <a href=\"guide/http#error-handling\">error handling</a>.</li>\n<li><a href=\"guide/http#testing-requests\">Testability</a> features.</li>\n<li>Request and response <a href=\"guide/http#intercepting-requests-and-responses\">interception</a>.</li>\n</ul>\n<h5 id=\"prerequisites\">Prerequisites<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#prerequisites\"><i class=\"material-icons\">link</i></a></h5>\n<p>Before working with the <code><a href=\"api/common/http/HttpClientModule\" class=\"code-anchor\">HttpClientModule</a></code>, you should have a basic understanding of the following:</p>\n<ul>\n<li>TypeScript programming</li>\n<li>Usage of the HTTP protocol</li>\n<li>Angular app-design fundamentals, as described in <a href=\"guide/architecture\">Angular Concepts</a></li>\n<li>Observable techniques and operators. See the <a href=\"guide/observables\">Observables</a> guide.</li>\n</ul>\n<h2 id=\"setup-for-server-communication\">Setup for server communication<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#setup-for-server-communication\"><i class=\"material-icons\">link</i></a></h2>\n<p>Before you can use <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code>, you need to import the Angular <code><a href=\"api/common/http/HttpClientModule\" class=\"code-anchor\">HttpClientModule</a></code>.\nMost apps do so in the root <code>AppModule</code>.</p>\n<code-example path=\"http/src/app/app.module.ts\" region=\"sketch\" header=\"app/app.module.ts (excerpt)\">\nimport { <a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a> } from '@angular/core';\nimport { <a href=\"api/platform-browser/BrowserModule\" class=\"code-anchor\">BrowserModule</a> } from '@angular/platform-browser';\nimport { <a href=\"api/common/http/HttpClientModule\" class=\"code-anchor\">HttpClientModule</a> } from '@angular/common/<a href=\"api/common/http\" class=\"code-anchor\">http</a>';\n\n@<a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a>({\n imports: [\n <a href=\"api/platform-browser/BrowserModule\" class=\"code-anchor\">BrowserModule</a>,\n // import <a href=\"api/common/http/HttpClientModule\" class=\"code-anchor\">HttpClientModule</a> after BrowserModule.\n <a href=\"api/common/http/HttpClientModule\" class=\"code-anchor\">HttpClientModule</a>,\n ],\n declarations: [\n AppComponent,\n ],\n bootstrap: [ AppComponent ]\n})\nexport class AppModule {}\n\n</code-example>\n<p>You can then inject the <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> service as a dependency of an application class, as shown in the following <code>ConfigService</code> example.</p>\n<code-example path=\"http/src/app/config/config.service.ts\" region=\"proto\" header=\"app/config/config.service.ts (excerpt)\">\nimport { <a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a> } from '@angular/core';\nimport { <a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a> } from '@angular/common/<a href=\"api/common/http\" class=\"code-anchor\">http</a>';\n\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()\nexport class ConfigService {\n constructor(private <a href=\"api/common/http\" class=\"code-anchor\">http</a>: <a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a>) { }\n}\n\n</code-example>\n<p>The <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> service makes use of <a href=\"guide/glossary#observable\" title=\"Observable definition\">observables</a> for all transactions. You must import the RxJS observable and operator symbols that appear in the example snippets. These <code>ConfigService</code> imports are typical.</p>\n<code-example path=\"http/src/app/config/config.service.ts\" region=\"rxjs-imports\" header=\"app/config/config.service.ts (RxJS imports)\">\nimport { Observable, throwError } from 'rxjs';\nimport { catchError, retry } from 'rxjs/operators';\n\n</code-example>\n<div class=\"alert is-helpful\">\n<p>You can run the <live-example></live-example> that accompanies this guide.</p>\n<p>The sample app does not require a data server.\nIt relies on the\n<a href=\"https://github.com/angular/angular/tree/master/packages/misc/angular-in-memory-web-api\">Angular <em>in-memory-web-api</em></a>,\nwhich replaces the <em>HttpClient</em> module's <code><a href=\"api/common/http/HttpBackend\" class=\"code-anchor\">HttpBackend</a></code>.\nThe replacement service simulates the behavior of a REST-like backend.</p>\n<p>Look at the <code>AppModule</code> <em>imports</em> to see how it is configured.</p>\n</div>\n<h2 id=\"requesting-data-from-a-server\">Requesting data from a server<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#requesting-data-from-a-server\"><i class=\"material-icons\">link</i></a></h2>\n<p>Use the <a href=\"api/common/http/HttpClient#get\"><code>HttpClient.get()</code></a> method to fetch data from a server.\nThe asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received.\nThe return type varies based on the <code>observe</code> and <code>responseType</code> values that you pass to the call.</p>\n<p>The <code>get()</code> method takes two arguments; the endpoint URL from which to fetch, and an <em>options</em> object that you can use to configure the request.</p>\n<code-example>\noptions: {\n headers?: <a href=\"api/common/http/HttpHeaders\" class=\"code-anchor\">HttpHeaders</a> | {[header: string]: string | string[]},\n observe?: 'body' | 'events' | 'response',\n params?: <a href=\"api/common/http/HttpParams\" class=\"code-anchor\">HttpParams</a>|{[param: string]: string | <a href=\"api/common/DecimalPipe\" class=\"code-anchor\">number</a> | boolean | ReadonlyArray<string | <a href=\"api/common/DecimalPipe\" class=\"code-anchor\">number</a> | boolean>},\n reportProgress?: boolean,\n responseType?: 'arraybuffer'|'blob'|'json'|'text',\n withCredentials?: boolean,\n }\n</code-example>\n<p>Important options include the <em>observe</em> and <em>responseType</em> properties.</p>\n<ul>\n<li>The <em>observe</em> option specifies how much of the response to return.</li>\n<li>The <em>responseType</em> option specifies the format in which to return data.</li>\n</ul>\n<div class=\"alert is-helpful\">\n<p>You can use the <code>options</code> object to configure various other aspects of an outgoing request.\nIn <a href=\"guide/http#adding-headers\">Adding headers</a>, for example, the service set the default headers using the <code>headers</code> option property.</p>\n<p>Use the <code>params</code> property to configure a request with <a href=\"guide/http#url-params\">HTTP URL parameters</a>, and the <code>reportProgress</code> option to <a href=\"guide/http#report-progress\">listen for progress events</a> when transferring large amounts of data.</p>\n</div>\n<p>Applications often request JSON data from a server.\nIn the <code>ConfigService</code> example, the app needs a configuration file on the server, <code>config.json</code>,\nthat specifies resource URLs.</p>\n<code-example path=\"http/src/assets/config.json\" header=\"assets/config.json\">\n{\n \"heroesUrl\": \"api/heroes\",\n \"textfile\": \"assets/textfile.txt\",\n \"date\": \"2020-01-29\"\n}\n\n\n</code-example>\n<p>To fetch this kind of data, the <code>get()</code> call needs the following options: <code>{observe: 'body', responseType: 'json'}</code>.\nThese are the default values for those options, so the following examples do not pass the options object.\nLater sections show some of the additional option possibilities.</p>\n<a id=\"config-service\"></a>\n<p>The example conforms to the best practices for creating scalable solutions by defining a re-usable <a href=\"guide/glossary#service\" title=\"service definition\">injectable service</a> to perform the data-handling functionality.\nIn addition to fetching data, the service can post-process the data, add error handling, and add retry logic.</p>\n<p>The <code>ConfigService</code> fetches this file using the <code><a href=\"api/common/http/HttpClient#get\" class=\"code-anchor\">HttpClient.get()</a></code> method.</p>\n<code-example path=\"http/src/app/config/config.service.ts\" region=\"getConfig_1\" header=\"app/config/config.service.ts (getConfig v.1)\">\nconfigUrl = 'assets/config.json';\n\ngetConfig() {\n return this.http.get(this.configUrl);\n}\n\n</code-example>\n<p>The <code>ConfigComponent</code> injects the <code>ConfigService</code> and calls\nthe <code>getConfig</code> service method.</p>\n<p>Because the service method returns an <code>Observable</code> of configuration data,\nthe component <em>subscribes</em> to the method's return value.\nThe subscription callback performs minimal post-processing.\nIt copies the data fields into the component's <code>config</code> object, which is data-bound in the component template for display.</p>\n<code-example path=\"http/src/app/config/config.component.ts\" region=\"v1\" header=\"app/config/config.component.ts (showConfig v.1)\">\nshowConfig() {\n this.configService.getConfig()\n .subscribe((data: Config) => this.config = {\n heroesUrl: data.heroesUrl,\n textfile: data.textfile,\n date: data.date,\n });\n}\n\n</code-example>\n<a id=\"typed-response\"></a>\n<h3 id=\"requesting-a-typed-response\">Requesting a typed response<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#requesting-a-typed-response\"><i class=\"material-icons\">link</i></a></h3>\n<p>You can structure your <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> request to declare the type of the response object, to make consuming the output easier and more obvious.\nSpecifying the response type acts as a type assertion at compile time.</p>\n<div class=\"alert is-important\">\n<p>Specifying the response type is a declaration to TypeScript that it should treat your response as being of the given type.\nThis is a build-time check and doesn't guarantee that the server will actually respond with an object of this type. It is up to the server to ensure that the type specified by the server API is returned.</p>\n</div>\n<p>To specify the response object type, first define an interface with the required properties.\nUse an interface rather than a class, because the response is a plain object that cannot be automatically converted to an instance of a class.</p>\n<code-example path=\"http/src/app/config/config.service.ts\" region=\"config-interface\">\nexport interface Config {\n heroesUrl: string;\n textfile: string;\n date: any;\n}\n\n</code-example>\n<p>Next, specify that interface as the <code><a href=\"api/common/http/HttpClient#get\" class=\"code-anchor\">HttpClient.get()</a></code> call's type parameter in the service.</p>\n<code-example path=\"http/src/app/config/config.service.ts\" region=\"getConfig_2\" header=\"app/config/config.service.ts (getConfig v.2)\">\ngetConfig() {\n // now returns an Observable of Config\n return this.http.get<Config>(this.configUrl);\n}\n\n</code-example>\n<div class=\"alert is-helpful\">\n<p> When you pass an interface as a type parameter to the <code><a href=\"api/common/http/HttpClient#get\" class=\"code-anchor\">HttpClient.get()</a></code> method, you can use the <a href=\"guide/rx-library#operators\">RxJS <code>map</code> operator</a> to transform the response data as needed by the UI. You can then pass the transformed data to the <a href=\"api/common/AsyncPipe\">async pipe</a>.</p>\n</div>\n<p>The callback in the updated component method receives a typed data object, which is\neasier and safer to consume:</p>\n<code-example path=\"http/src/app/config/config.component.ts\" region=\"v2\" header=\"app/config/config.component.ts (showConfig v.2)\">\nconfig: Config;\n\nshowConfig() {\n this.configService.getConfig()\n // clone the data object, using its known Config shape\n .subscribe((data: Config) => this.config = { ...data });\n}\n\n</code-example>\n<p>To access properties that are defined in an interface, you must explicitly convert the plain object you get from the JSON to the required response type.\nFor example, the following <code>subscribe</code> callback receives <code>data</code> as an Object, and then type-casts it in order to access the properties.</p>\n<code-example>\n .subscribe(data => this.config = {\n heroesUrl: (data as any).heroesUrl,\n textfile: (data as any).textfile,\n });\n</code-example>\n<a id=\"string-union-types\"></a>\n<div class=\"callout is-important\">\n<header>*observe* and *response* types</header>\n<p>The types of the <code>observe</code> and <code>response</code> options are <em>string unions</em>, rather than plain strings.</p>\n<code-example>\noptions: {\n ...\n observe?: 'body' | 'events' | 'response',\n ...\n responseType?: 'arraybuffer'|'blob'|'json'|'text',\n ...\n }\n</code-example>\n<p>This can cause confusion. For example:</p>\n<code-example language=\"typescript\">\n// this works\nclient.get('/foo', {responseType: 'text'})\n\n// but this does NOT work\nconst options = {\n responseType: 'text',\n};\nclient.get('/foo', options)\n</code-example>\n<p>In the second case, TypeScript infers the type of <code>options</code> to be <code>{responseType: string}</code>.\nThe type is too wide to pass to <code>HttpClient.get</code> which is expecting the type of <code>responseType</code> to be one of the <em>specific</em> strings.\n<code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> is typed explicitly this way so that the compiler can report the correct return type based on the options you provided.</p>\n<p>Use <code>as const</code> to let TypeScript know that you really do mean to use a constant string type:</p>\n<code-example language=\"typescript\">\nconst options = {\n responseType: 'text' as const,\n};\nclient.get('/foo', options);\n</code-example>\n</div>\n<h3 id=\"reading-the-full-response\">Reading the full response<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#reading-the-full-response\"><i class=\"material-icons\">link</i></a></h3>\n<p>In the previous example, the call to <code><a href=\"api/common/http/HttpClient#get\" class=\"code-anchor\">HttpClient.get()</a></code> did not specify any options. By default, it returned the JSON data contained in the response body.</p>\n<p>You might need more information about the transaction than is contained in the response body. Sometimes servers return special headers or status codes to indicate certain conditions that are important to the application workflow.</p>\n<p>Tell <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> that you want the full response with the <code>observe</code> option of the <code>get()</code> method:</p>\n<code-example path=\"http/src/app/config/config.service.ts\" region=\"getConfigResponse\">\ngetConfigResponse(): Observable<<a href=\"api/common/http/HttpResponse\" class=\"code-anchor\">HttpResponse</a><Config>> {\n return this.http.get<Config>(\n this.configUrl, { observe: 'response' });\n}\n\n</code-example>\n<p>Now <code><a href=\"api/common/http/HttpClient#get\" class=\"code-anchor\">HttpClient.get()</a></code> returns an <code>Observable</code> of type <code><a href=\"api/common/http/HttpResponse\" class=\"code-anchor\">HttpResponse</a></code> rather than just the JSON data contained in the body.</p>\n<p>The component's <code>showConfigResponse()</code> method displays the response headers as well as the configuration:</p>\n<code-example path=\"http/src/app/config/config.component.ts\" region=\"showConfigResponse\" header=\"app/config/config.component.ts (showConfigResponse)\">\nshowConfigResponse() {\n this.configService.getConfigResponse()\n // resp is of type `<a href=\"api/common/http/HttpResponse\" class=\"code-anchor\">HttpResponse</a><Config>`\n .subscribe(resp => {\n // display its headers\n const keys = resp.headers.keys();\n this.headers = keys.map(key =>\n `${key}: ${resp.headers.get(key)}`);\n\n // access the body directly, which is typed as `Config`.\n this.config = { ... resp.body };\n });\n}\n\n</code-example>\n<p>As you can see, the response object has a <code>body</code> property of the correct type.</p>\n<h3 id=\"making-a-jsonp-request\">Making a JSONP request<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#making-a-jsonp-request\"><i class=\"material-icons\">link</i></a></h3>\n<p>Apps can use the <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> to make <a href=\"https://en.wikipedia.org/wiki/JSONP\">JSONP</a> requests across domains when a server doesn't support <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS\">CORS protocol</a>.</p>\n<p>Angular JSONP requests return an <code>Observable</code>.\nFollow the pattern for subscribing to observables and use the RxJS <code>map</code> operator to transform the response before using the <a href=\"api/common/AsyncPipe\">async pipe</a> to manage the results.</p>\n<p>In Angular, use JSONP by including <code><a href=\"api/common/http/HttpClientJsonpModule\" class=\"code-anchor\">HttpClientJsonpModule</a></code> in the <code><a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a></code> imports.\nIn the following example, the <code>searchHeroes()</code> method uses a JSONP request to query for heroes whose names contain the search term.</p>\n<code-example language=\"ts\">\n/* GET heroes whose name contains search term */\nsearchHeroes(term: string): Observable {\n term = term.trim();\n\n const heroesURL = `${this.heroesURL}?${term}`;\n return this.http.jsonp(heroesUrl, 'callback').pipe(\n catchError(this.handleError('searchHeroes', [])) // then handle the error\n );\n}\n</code-example>\n<p>This request passes the <code>heroesURL</code> as the first parameter and the callback function name as the second parameter.\nThe response is wrapped in the callback function, which takes the observables returned by the JSONP method and pipes them through to the error handler.</p>\n<h3 id=\"requesting-non-json-data\">Requesting non-JSON data<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#requesting-non-json-data\"><i class=\"material-icons\">link</i></a></h3>\n<p>Not all APIs return JSON data.\nIn this next example, a <code>DownloaderService</code> method reads a text file from the server and logs the file contents, before returning those contents to the caller as an <code>Observable<string></code>.</p>\n<code-example path=\"http/src/app/downloader/downloader.service.ts\" region=\"getTextFile\" header=\"app/downloader/downloader.service.ts (getTextFile)\" linenums=\"false\">\ngetTextFile(filename: string) {\n // The Observable returned by get() is of type Observable<string>\n // because a text response was specified.\n // There's no need to pass a <string> type parameter to get().\n return this.http.get(filename, {responseType: 'text'})\n .pipe(\n tap( // Log the result or error\n data => this.log(filename, data),\n error => this.logError(filename, error)\n )\n );\n}\n\n</code-example>\n<p><code><a href=\"api/common/http/HttpClient#get\" class=\"code-anchor\">HttpClient.get()</a></code> returns a string rather than the default JSON because of the <code>responseType</code> option.</p>\n<p>The RxJS <code>tap</code> operator (as in \"wiretap\") lets the code inspect both success and error values passing through the observable without disturbing them.</p>\n<p>A <code>download()</code> method in the <code>DownloaderComponent</code> initiates the request by subscribing to the service method.</p>\n<code-example path=\"http/src/app/downloader/downloader.component.ts\" region=\"download\" header=\"app/downloader/downloader.component.ts (download)\" linenums=\"false\">\ndownload() {\n this.downloaderService.getTextFile('assets/textfile.txt')\n .subscribe(results => this.contents = results);\n}\n\n</code-example>\n<a id=\"error-handling\"></a>\n<h2 id=\"handling-request-errors\">Handling request errors<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#handling-request-errors\"><i class=\"material-icons\">link</i></a></h2>\n<p>If the request fails on the server, <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> returns an <em>error</em> object instead of a successful response.</p>\n<p>The same service that performs your server transactions should also perform error inspection, interpretation, and resolution.</p>\n<p>When an error occurs, you can obtain details of what failed in order to inform your user. In some cases, you might also automatically <a href=\"guide/http#retry\">retry the request</a>.</p>\n<a id=\"error-details\"></a>\n<h3 id=\"getting-error-details\">Getting error details<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#getting-error-details\"><i class=\"material-icons\">link</i></a></h3>\n<p>An app should give the user useful feedback when data access fails.\nA raw error object is not particularly useful as feedback.\nIn addition to detecting that an error has occurred, you need to get error details and use those details to compose a user-friendly response.</p>\n<p>Two types of errors can occur.</p>\n<ul>\n<li>\n<p>The server backend might reject the request, returning an HTTP response with a status code such as 404 or 500. These are error <em>responses</em>.</p>\n</li>\n<li>\n<p>Something could go wrong on the client-side such as a network error that prevents the request from completing successfully or an exception thrown in an RxJS operator. These errors produce JavaScript <code>ErrorEvent</code> objects.</p>\n</li>\n</ul>\n<p><code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> captures both kinds of errors in its <code><a href=\"api/common/http/HttpErrorResponse\" class=\"code-anchor\">HttpErrorResponse</a></code>. You can inspect that response to identify the error's cause.</p>\n<p>The following example defines an error handler in the previously defined <a href=\"guide/http#config-service\" title=\"ConfigService defined\">ConfigService</a>.</p>\n<code-example path=\"http/src/app/config/config.service.ts\" region=\"handleError\" header=\"app/config/config.service.ts (handleError)\">\nprivate handleError(error: <a href=\"api/common/http/HttpErrorResponse\" class=\"code-anchor\">HttpErrorResponse</a>) {\n if (error.error instanceof ErrorEvent) {\n // A client-side or network error occurred. Handle it accordingly.\n console.error('An error occurred:', error.error.message);\n } else {\n // The backend returned an unsuccessful response code.\n // The response body may contain clues as to what went wrong.\n console.error(\n `Backend returned code ${error.status}, ` +\n `body was: ${error.error}`);\n }\n // Return an observable with a user-facing error message.\n return throwError(\n 'Something bad happened; please try again later.');\n}\n\n</code-example>\n<p>The handler returns an RxJS <code>ErrorObservable</code> with a user-friendly error message.\nThe following code updates the <code>getConfig()</code> method, using a <a href=\"guide/pipes\" title=\"Pipes guide\">pipe</a> to send all observables returned by the <code><a href=\"api/common/http/HttpClient#get\" class=\"code-anchor\">HttpClient.get()</a></code> call to the error handler.</p>\n<code-example path=\"http/src/app/config/config.service.ts\" region=\"getConfig_3\" header=\"app/config/config.service.ts (getConfig v.3 with error handler)\">\ngetConfig() {\n return this.http.get<Config>(this.configUrl)\n .pipe(\n catchError(this.handleError)\n );\n}\n\n</code-example>\n<a id=\"retry\"></a>\n<h3 id=\"retrying-a-failed-request\">Retrying a failed request<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#retrying-a-failed-request\"><i class=\"material-icons\">link</i></a></h3>\n<p>Sometimes the error is transient and goes away automatically if you try again.\nFor example, network interruptions are common in mobile scenarios, and trying again\ncan produce a successful result.</p>\n<p>The <a href=\"guide/rx-library\">RxJS library</a> offers several <em>retry</em> operators.\nFor example, the <code>retry()</code> operator automatically re-subscribes to a failed <code>Observable</code> a specified number of times. <em>Re-subscribing</em> to the result of an <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> method call has the effect of reissuing the HTTP request.</p>\n<p>The following example shows how you can pipe a failed request to the <code>retry()</code> operator before passing it to the error handler.</p>\n<code-example path=\"http/src/app/config/config.service.ts\" region=\"getConfig\" header=\"app/config/config.service.ts (getConfig with retry)\">\ngetConfig() {\n return this.http.get<Config>(this.configUrl)\n .pipe(\n retry(3), // retry a failed request up to 3 times\n catchError(this.handleError) // then handle the error\n );\n}\n\n</code-example>\n<h2 id=\"sending-data-to-a-server\">Sending data to a server<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#sending-data-to-a-server\"><i class=\"material-icons\">link</i></a></h2>\n<p>In addition to fetching data from a server, <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> supports other HTTP methods such as PUT, POST, and DELETE, which you can use to modify the remote data.</p>\n<p>The sample app for this guide includes a simplified version of the \"Tour of Heroes\" example\nthat fetches heroes and enables users to add, delete, and update them.\nThe following sections show examples of the data-update methods from the sample's <code>HeroesService</code>.</p>\n<h3 id=\"making-a-post-request\">Making a POST request<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#making-a-post-request\"><i class=\"material-icons\">link</i></a></h3>\n<p>Apps often send data to a server with a POST request when submitting a form.\nIn the following example, the <code>HeroesService</code> makes an HTTP POST request when adding a hero to the database.</p>\n<code-example path=\"http/src/app/heroes/heroes.service.ts\" region=\"addHero\" header=\"app/heroes/heroes.service.ts (addHero)\">\n/** POST: add a new hero to the database */\naddHero(hero: Hero): Observable<Hero> {\n return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)\n .pipe(\n catchError(this.handleError('addHero', hero))\n );\n}\n\n</code-example>\n<p>The <code><a href=\"api/common/http/HttpClient#post\" class=\"code-anchor\">HttpClient.post()</a></code> method is similar to <code>get()</code> in that it has a type parameter, which you can use to specify that you expect the server to return data of a given type. The method takes a resource URL and two additional parameters:</p>\n<ul>\n<li><em>body</em> - The data to POST in the body of the request.</li>\n<li><em>options</em> - An object containing method options which, in this case, <a href=\"guide/http#adding-headers\">specify required headers</a>.</li>\n</ul>\n<p>The example catches errors as <a href=\"guide/http#error-details\">described above</a>.</p>\n<p>The <code>HeroesComponent</code> initiates the actual POST operation by subscribing to\nthe <code>Observable</code> returned by this service method.</p>\n<code-example path=\"http/src/app/heroes/heroes.component.ts\" region=\"add-hero-subscribe\" header=\"app/heroes/heroes.component.ts (addHero)\">\nthis.heroesService\n .addHero(newHero)\n .subscribe(hero => this.heroes.push(hero));\n\n</code-example>\n<p>When the server responds successfully with the newly added hero, the component adds\nthat hero to the displayed <code>heroes</code> list.</p>\n<h3 id=\"making-a-delete-request\">Making a DELETE request<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#making-a-delete-request\"><i class=\"material-icons\">link</i></a></h3>\n<p>This application deletes a hero with the <code>HttpClient.delete</code> method by passing the hero's id\nin the request URL.</p>\n<code-example path=\"http/src/app/heroes/heroes.service.ts\" region=\"deleteHero\" header=\"app/heroes/heroes.service.ts (deleteHero)\">\n/** DELETE: delete the hero from the server */\ndeleteHero(id: number): Observable<{}> {\n const url = `${this.heroesUrl}/${id}`; // DELETE api/heroes/42\n return this.http.delete(url, httpOptions)\n .pipe(\n catchError(this.handleError('deleteHero'))\n );\n}\n\n</code-example>\n<p>The <code>HeroesComponent</code> initiates the actual DELETE operation by subscribing to\nthe <code>Observable</code> returned by this service method.</p>\n<code-example path=\"http/src/app/heroes/heroes.component.ts\" region=\"delete-hero-subscribe\" header=\"app/heroes/heroes.component.ts (deleteHero)\">\nthis.heroesService\n .deleteHero(hero.id)\n .subscribe();\n\n</code-example>\n<p>The component isn't expecting a result from the delete operation, so it subscribes without a callback. Even though you are not using the result, you still have to subscribe. Calling the <code>subscribe()</code> method <em>executes</em> the observable, which is what initiates the DELETE request.</p>\n<div class=\"alert is-important\">\n<p>You must call <em>subscribe()</em> or nothing happens. Just calling <code>HeroesService.deleteHero()</code> does not initiate the DELETE request.</p>\n</div>\n<code-example path=\"http/src/app/heroes/heroes.component.ts\" region=\"delete-hero-no-subscribe\">\n// oops ... subscribe() is missing so nothing happens\nthis.heroesService.deleteHero(hero.id);\n\n</code-example>\n<a id=\"always-subscribe\"></a>\n<p><strong>Always <em>subscribe</em>!</strong></p>\n<p>An <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> method does not begin its HTTP request until you call <code>subscribe()</code> on the observable returned by that method. This is true for <em>all</em> <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> <em>methods</em>.</p>\n<div class=\"alert is-helpful\">\n<p>The <a href=\"api/common/AsyncPipe\"><code>AsyncPipe</code></a> subscribes (and unsubscribes) for you automatically.</p>\n</div>\n<p>All observables returned from <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> methods are <em>cold</em> by design.\nExecution of the HTTP request is <em>deferred</em>, allowing you to extend the\nobservable with additional operations such as <code>tap</code> and <code>catchError</code> before anything actually happens.</p>\n<p>Calling <code>subscribe(...)</code> triggers execution of the observable and causes\n<code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> to compose and send the HTTP request to the server.</p>\n<p>You can think of these observables as <em>blueprints</em> for actual HTTP requests.</p>\n<div class=\"alert is-helpful\">\n<p>In fact, each <code>subscribe()</code> initiates a separate, independent execution of the observable.\nSubscribing twice results in two HTTP requests.</p>\n<code-example language=\"javascript\">\nconst req = http.get<Heroes>('/api/heroes');\n// 0 requests made - .subscribe() not called.\nreq.subscribe();\n// 1 request made.\nreq.subscribe();\n// 2 requests made.\n</code-example>\n</div>\n<h3 id=\"making-a-put-request\">Making a PUT request<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#making-a-put-request\"><i class=\"material-icons\">link</i></a></h3>\n<p>An app can send PUT requests using the HTTP client service.\nThe following <code>HeroesService</code> example, like the POST example, replaces a resource with updated data.</p>\n<code-example path=\"http/src/app/heroes/heroes.service.ts\" region=\"updateHero\" header=\"app/heroes/heroes.service.ts (updateHero)\">\n/** PUT: update the hero on the server. Returns the updated hero upon success. */\nupdateHero(hero: Hero): Observable<Hero> {\n return this.http.put<Hero>(this.heroesUrl, hero, httpOptions)\n .pipe(\n catchError(this.handleError('updateHero', hero))\n );\n}\n\n</code-example>\n<p>As for any of the HTTP methods that return an observable, the caller, <code>HeroesComponent.update()</code> <a href=\"guide/http#always-subscribe\" title=\"Why you must always subscribe.\">must <code>subscribe()</code></a> to the observable returned from the <code><a href=\"api/common/http/HttpClient#put\" class=\"code-anchor\">HttpClient.put()</a></code> in order to initiate the request.</p>\n<h3 id=\"adding-and-updating-headers\">Adding and updating headers<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#adding-and-updating-headers\"><i class=\"material-icons\">link</i></a></h3>\n<p>Many servers require extra headers for save operations.\nFor example, a server might require an authorization token, or \"Content-Type\" header to explicitly declare the MIME type of the request body.</p>\n<h5 id=\"adding-headers\">Adding headers<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#adding-headers\"><i class=\"material-icons\">link</i></a></h5>\n<p>The <code>HeroesService</code> defines such headers in an <code>httpOptions</code> object that are passed\nto every <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> save method.</p>\n<code-example path=\"http/src/app/heroes/heroes.service.ts\" region=\"http-options\" header=\"app/heroes/heroes.service.ts (httpOptions)\">\nimport { <a href=\"api/common/http/HttpHeaders\" class=\"code-anchor\">HttpHeaders</a> } from '@angular/common/<a href=\"api/common/http\" class=\"code-anchor\">http</a>';\n\nconst httpOptions = {\n headers: new <a href=\"api/common/http/HttpHeaders\" class=\"code-anchor\">HttpHeaders</a>({\n 'Content-Type': 'application/json',\n Authorization: 'my-auth-token'\n })\n};\n\n</code-example>\n<h5 id=\"updating-headers\">Updating headers<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#updating-headers\"><i class=\"material-icons\">link</i></a></h5>\n<p>You can't directly modify the existing headers within the previous options\nobject because instances of the <code><a href=\"api/common/http/HttpHeaders\" class=\"code-anchor\">HttpHeaders</a></code> class are immutable.\nUse the <code>set()</code> method instead, to return a clone of the current instance with the new changes applied.</p>\n<p>The following example shows how, when an old token has expired, you can update the authorization header before making the next request.</p>\n<code-example path=\"http/src/app/heroes/heroes.service.ts\" region=\"update-headers\" linenums=\"false\">\nhttpOptions.headers =\n httpOptions.headers.set('Authorization', 'my-new-auth-token');\n\n</code-example>\n<a id=\"url-params\"></a>\n<h2 id=\"configuring-http-url-parameters\">Configuring HTTP URL parameters<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#configuring-http-url-parameters\"><i class=\"material-icons\">link</i></a></h2>\n<p>Use the <code><a href=\"api/common/http/HttpParams\" class=\"code-anchor\">HttpParams</a></code> class with the <code>params</code> request option to add URL query strings in your <code><a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a></code>.</p>\n<p>The following example, the <code>searchHeroes()</code> method queries for heroes whose names contain the search term.</p>\n<p>Start by importing <code><a href=\"api/common/http/HttpParams\" class=\"code-anchor\">HttpParams</a></code> class.</p>\n<code-example hidecopy=\"\" language=\"typescript\">\nimport {<a href=\"api/common/http/HttpParams\" class=\"code-anchor\">HttpParams</a>} from \"@angular/common/<a href=\"api/common/http\" class=\"code-anchor\">http</a>\";\n</code-example>\n<code-example path=\"http/src/app/heroes/heroes.service.ts\" region=\"searchHeroes\" linenums=\"false\">\n/* GET heroes whose name contains search term */\nsearchHeroes(term: string): Observable<Hero[]> {\n term = term.trim();\n\n // Add safe, URL encoded search parameter if there is a search term\n const options = term ?\n { params: new <a href=\"api/common/http/HttpParams\" class=\"code-anchor\">HttpParams</a>().set('name', term) } : {};\n\n return this.http.get<Hero[]>(this.heroesUrl, options)\n .pipe(\n catchError(this.handleError<Hero[]>('searchHeroes', []))\n );\n}\n\n</code-example>\n<p>If there is a search term, the code constructs an options object with an HTML URL-encoded search parameter.\nIf the term is \"cat\", for example, the GET request URL would be <code>api/heroes?name=cat</code>.</p>\n<p>The <code><a href=\"api/common/http/HttpParams\" class=\"code-anchor\">HttpParams</a></code> object is immutable. If you need to update the options, save the returned value of the <code>.set()</code> method.</p>\n<p>You can also create HTTP parameters directly from a query string by using the <code>fromString</code> variable:</p>\n<code-example hidecopy=\"\" language=\"typescript\">\nconst params = new <a href=\"api/common/http/HttpParams\" class=\"code-anchor\">HttpParams</a>({fromString: 'name=foo'});\n</code-example>\n<a id=\"intercepting-requests-and-responses\"></a>\n<h2 id=\"intercepting-requests-and-responses\">Intercepting requests and responses<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#intercepting-requests-and-responses\"><i class=\"material-icons\">link</i></a></h2>\n<p>With interception, you declare <em>interceptors</em> that inspect and transform HTTP requests from your application to a server.\nThe same interceptors can also inspect and transform a server's responses on their way back to the application.\nMultiple interceptors form a <em>forward-and-backward</em> chain of request/response handlers.</p>\n<p>Interceptors can perform a variety of <em>implicit</em> tasks, from authentication to logging, in a routine, standard way, for every HTTP request/response.</p>\n<p>Without interception, developers would have to implement these tasks <em>explicitly</em>\nfor each <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> method call.</p>\n<h3 id=\"write-an-interceptor\">Write an interceptor<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#write-an-interceptor\"><i class=\"material-icons\">link</i></a></h3>\n<p>To implement an interceptor, declare a class that implements the <code>intercept()</code> method of the <code><a href=\"api/common/http/HttpInterceptor\" class=\"code-anchor\">HttpInterceptor</a></code> interface.</p>\n<p> Here is a do-nothing <em>noop</em> interceptor that simply passes the request through without touching it:\n<code-example path=\"http/src/app/http-interceptors/noop-interceptor.ts\" header=\"app/http-interceptors/noop-interceptor.ts\">\nimport { <a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a> } from '@angular/core';\nimport {\n <a href=\"api/common/http/HttpEvent\" class=\"code-anchor\">HttpEvent</a>, <a href=\"api/common/http/HttpInterceptor\" class=\"code-anchor\">HttpInterceptor</a>, <a href=\"api/common/http/HttpHandler\" class=\"code-anchor\">HttpHandler</a>, <a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a>\n} from '@angular/common/<a href=\"api/common/http\" class=\"code-anchor\">http</a>';\n\nimport { Observable } from 'rxjs';\n\n/** Pass untouched request through to the next request handler. */\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()\nexport class NoopInterceptor implements <a href=\"api/common/http/HttpInterceptor\" class=\"code-anchor\">HttpInterceptor</a> {\n\n intercept(req: <a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a><any>, next: <a href=\"api/common/http/HttpHandler\" class=\"code-anchor\">HttpHandler</a>):\n Observable<<a href=\"api/common/http/HttpEvent\" class=\"code-anchor\">HttpEvent</a><any>> {\n return next.handle(req);\n }\n}\n\n\n</code-example></p>\n<p>The <code>intercept</code> method transforms a request into an <code>Observable</code> that eventually returns the HTTP response.\nIn this sense, each interceptor is fully capable of handling the request entirely by itself.</p>\n<p>Most interceptors inspect the request on the way in and forward the (perhaps altered) request to the <code>handle()</code> method of the <code>next</code> object which implements the <a href=\"api/common/http/HttpHandler\"><code>HttpHandler</code></a> interface.</p>\n<code-example language=\"javascript\">\nexport abstract class <a href=\"api/common/http/HttpHandler\" class=\"code-anchor\">HttpHandler</a> {\n abstract handle(req: <a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a><any>): Observable<<a href=\"api/common/http/HttpEvent\" class=\"code-anchor\">HttpEvent</a><any>>;\n}\n</code-example>\n<p>Like <code>intercept()</code>, the <code>handle()</code> method transforms an HTTP request into an <code>Observable</code> of <a href=\"guide/http#interceptor-events\"><code>HttpEvents</code></a> which ultimately include the server's response. The <code>intercept()</code> method could inspect that observable and alter it before returning it to the caller.</p>\n<p>This <em>no-op</em> interceptor simply calls <code>next.handle()</code> with the original request and returns the observable without doing a thing.</p>\n<h3 id=\"the-next-object\">The <em>next</em> object<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#the-next-object\"><i class=\"material-icons\">link</i></a></h3>\n<p>The <code>next</code> object represents the next interceptor in the chain of interceptors.\nThe final <code>next</code> in the chain is the <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> backend handler that sends the request to the server and receives the server's response.</p>\n<p>Most interceptors call <code>next.handle()</code> so that the request flows through to the next interceptor and, eventually, the backend handler.\nAn interceptor <em>could</em> skip calling <code>next.handle()</code>, short-circuit the chain, and <a href=\"guide/http#caching\">return its own <code>Observable</code></a> with an artificial server response.</p>\n<p>This is a common middleware pattern found in frameworks such as Express.js.</p>\n<h3 id=\"provide-the-interceptor\">Provide the interceptor<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#provide-the-interceptor\"><i class=\"material-icons\">link</i></a></h3>\n<p>The <code>NoopInterceptor</code> is a service managed by Angular's <a href=\"guide/dependency-injection\">dependency injection (DI)</a> system.\nLike other services, you must provide the interceptor class before the app can use it.</p>\n<p>Because interceptors are (optional) dependencies of the <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> service,\nyou must provide them in the same injector (or a parent of the injector) that provides <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code>.\nInterceptors provided <em>after</em> DI creates the <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> are ignored.</p>\n<p>This app provides <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> in the app's root injector, as a side-effect of importing the <code><a href=\"api/common/http/HttpClientModule\" class=\"code-anchor\">HttpClientModule</a></code> in <code>AppModule</code>.\nYou should provide interceptors in <code>AppModule</code> as well.</p>\n<p>After importing the <code><a href=\"api/common/http/HTTP_INTERCEPTORS\" class=\"code-anchor\">HTTP_INTERCEPTORS</a></code> injection token from <code>@angular/common/<a href=\"api/common/http\" class=\"code-anchor\">http</a></code>,\nwrite the <code>NoopInterceptor</code> provider like this:</p>\n<code-example path=\"http/src/app/http-interceptors/index.ts\" region=\"noop-provider\">\n{ provide: <a href=\"api/common/http/HTTP_INTERCEPTORS\" class=\"code-anchor\">HTTP_INTERCEPTORS</a>, useClass: NoopInterceptor, multi: true },\n\n</code-example>\n<p>Note the <code>multi: true</code> option.\nThis required setting tells Angular that <code><a href=\"api/common/http/HTTP_INTERCEPTORS\" class=\"code-anchor\">HTTP_INTERCEPTORS</a></code> is a token for a <em>multiprovider</em>\nthat injects an array of values, rather than a single value.</p>\n<p>You <em>could</em> add this provider directly to the providers array of the <code>AppModule</code>.\nHowever, it's rather verbose and there's a good chance that\nyou'll create more interceptors and provide them in the same way.\nYou must also pay <a href=\"guide/http#interceptor-order\">close attention to the order</a>\nin which you provide these interceptors.</p>\n<p>Consider creating a \"barrel\" file that gathers all the interceptor providers into an <code>httpInterceptorProviders</code> array, starting with this first one, the <code>NoopInterceptor</code>.</p>\n<code-example path=\"http/src/app/http-interceptors/index.ts\" region=\"interceptor-providers\" header=\"app/http-interceptors/index.ts\">\n/* \"Barrel\" of Http Interceptors */\nimport { <a href=\"api/common/http/HTTP_INTERCEPTORS\" class=\"code-anchor\">HTTP_INTERCEPTORS</a> } from '@angular/common/<a href=\"api/common/http\" class=\"code-anchor\">http</a>';\n\nimport { NoopInterceptor } from './noop-interceptor';\n\n/** Http interceptor providers in outside-in order */\nexport const httpInterceptorProviders = [\n { provide: <a href=\"api/common/http/HTTP_INTERCEPTORS\" class=\"code-anchor\">HTTP_INTERCEPTORS</a>, useClass: NoopInterceptor, multi: true },\n];\n\n</code-example>\n<p>Then import and add it to the <code>AppModule</code> <em>providers array</em> like this:</p>\n<code-example path=\"http/src/app/app.module.ts\" region=\"interceptor-providers\" header=\"app/app.module.ts (interceptor providers)\">\nproviders: [\n httpInterceptorProviders\n],\n\n</code-example>\n<p>As you create new interceptors, add them to the <code>httpInterceptorProviders</code> array and\nyou won't have to revisit the <code>AppModule</code>.</p>\n<div class=\"alert is-helpful\">\n<p>There are many more interceptors in the complete sample code.</p>\n</div>\n<h3 id=\"interceptor-order\">Interceptor order<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#interceptor-order\"><i class=\"material-icons\">link</i></a></h3>\n<p>Angular applies interceptors in the order that you provide them.\nFor example, consider a situation in which you want to handle the authentication of your HTTP requests and log them before sending them to a server. To accomplish this task, you could provide an <code>AuthInterceptor</code> service and then a <code>LoggingInterceptor</code> service.\nOutgoing requests would flow from the <code>AuthInterceptor</code> to the <code>LoggingInterceptor</code>.\nResponses from these requests would flow in the other direction, from <code>LoggingInterceptor</code> back to <code>AuthInterceptor</code>.\nThe following is a visual representation of the process:</p>\n<div class=\"lightbox\">\n <img src=\"generated/images/guide/http/interceptor-order.svg\" alt=\"Interceptor order\" width=\"300\" height=\"522\">\n</div>\n<div class=\"alert is-helpful\">\n<p> The last interceptor in the process is always the <code><a href=\"api/common/http/HttpBackend\" class=\"code-anchor\">HttpBackend</a></code> that handles communication with the server.</p>\n</div>\n<p>You cannot change the order or remove interceptors later.\nIf you need to enable and disable an interceptor dynamically, you'll have to build that capability into the interceptor itself.</p>\n<a id=\"interceptor-events\"></a>\n<h3 id=\"handling-interceptor-events\">Handling interceptor events<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#handling-interceptor-events\"><i class=\"material-icons\">link</i></a></h3>\n<p>Most <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> methods return observables of <code><a href=\"api/common/http/HttpResponse\" class=\"code-anchor\">HttpResponse</a><any></code>.\nThe <code><a href=\"api/common/http/HttpResponse\" class=\"code-anchor\">HttpResponse</a></code> class itself is actually an event, whose type is <code><a href=\"api/common/http/HttpEventType#Response\" class=\"code-anchor\">HttpEventType.Response</a></code>.\nA single HTTP request can, however, generate multiple events of other types, including upload and download progress events.\nThe methods <code>HttpInterceptor.intercept()</code> and <code>HttpHandler.handle()</code> return observables of <code><a href=\"api/common/http/HttpEvent\" class=\"code-anchor\">HttpEvent</a><any></code>.</p>\n<p>Many interceptors are only concerned with the outgoing request and return the event stream from <code>next.handle()</code> without modifying it.\nSome interceptors, however, need to examine and modify the response from <code>next.handle()</code>; these operations can see all of these events in the stream.</p>\n<a id=\"immutability\"></a>\n<p>Although interceptors are capable of modifying requests and responses,\nthe <code><a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a></code> and <code><a href=\"api/common/http/HttpResponse\" class=\"code-anchor\">HttpResponse</a></code> instance properties are <code>readonly</code>,\nrendering them largely immutable.\nThey are immutable for a good reason: an app might retry a request several times before it succeeds, which means that the interceptor chain can re-process the same request multiple times.\nIf an interceptor could modify the original request object, the re-tried operation would start from the modified request rather than the original. Immutability ensures that interceptors see the same request for each try.</p>\n<div class=\"alert is-helpful\">\n<p> Your interceptor should return every event without modification unless it has a compelling reason to do otherwise.</p>\n</div>\n<p>TypeScript prevents you from setting <code><a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a></code> read-only properties.</p>\n<code-example language=\"javascript\">\n // Typescript disallows the following assignment because req.url is readonly\n req.url = req.url.replace('<a href=\"api/common/http\" class=\"code-anchor\">http</a>://', 'https://');\n</code-example>\n<p>If you must alter a request, clone it first and modify the clone before passing it to <code>next.handle()</code>.\nYou can clone and modify the request in a single step, as shown in the following example.</p>\n<code-example path=\"http/src/app/http-interceptors/ensure-https-interceptor.ts\" region=\"excerpt\" header=\"app/http-interceptors/ensure-https-interceptor.ts (excerpt)\">\n// clone request and replace '<a href=\"api/common/http\" class=\"code-anchor\">http</a>://' with 'https://' at the same time\nconst secureReq = req.clone({\n url: req.url.replace('<a href=\"api/common/http\" class=\"code-anchor\">http</a>://', 'https://')\n});\n// send the cloned, \"secure\" request to the next handler.\nreturn next.handle(secureReq);\n\n</code-example>\n<p>The <code>clone()</code> method's hash argument allows you to mutate specific properties of the request while copying the others.</p>\n<h4 id=\"modifying-a-request-body\">Modifying a request body<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#modifying-a-request-body\"><i class=\"material-icons\">link</i></a></h4>\n<p>The <code>readonly</code> assignment guard can't prevent deep updates and, in particular,\nit can't prevent you from modifying a property of a request body object.</p>\n<code-example language=\"javascript\">\n req.body.name = req.body.name.trim(); // bad idea!\n</code-example>\n<p>If you must modify the request body, follow these steps.</p>\n<ol>\n<li>Copy the body and make your change in the copy.</li>\n<li>Clone the request object, using its <code>clone()</code> method.</li>\n<li>Replace the clone's body with the modified copy.</li>\n</ol>\n<code-example path=\"http/src/app/http-interceptors/trim-name-interceptor.ts\" region=\"excerpt\" header=\"app/http-interceptors/trim-name-interceptor.ts (excerpt)\">\n// copy the body and trim whitespace from the name property\nconst newBody = { ...body, name: body.name.trim() };\n// clone request and set its body\nconst newReq = req.clone({ body: newBody });\n// send the cloned request to the next handler.\nreturn next.handle(newReq);\n\n</code-example>\n<h4 id=\"clearing-the-request-body-in-a-clone\">Clearing the request body in a clone<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#clearing-the-request-body-in-a-clone\"><i class=\"material-icons\">link</i></a></h4>\n<p>Sometimes you need to clear the request body rather than replace it.\nTo do this, set the cloned request body to <code>null</code>.</p>\n<div class=\"alert is-helpful\">\n<p><strong>Tip</strong>: If you set the cloned request body to <code>undefined</code>, Angular assumes you intend to leave the body as is.</p>\n</div>\n<code-example language=\"javascript\">\n newReq = req.clone({ ... }); // body not mentioned => preserve original body\n newReq = req.clone({ body: undefined }); // preserve original body\n newReq = req.clone({ body: null }); // clear the body\n</code-example>\n<h2 id=\"http-interceptor-use-cases\">Http interceptor use-cases<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#http-interceptor-use-cases\"><i class=\"material-icons\">link</i></a></h2>\n<p>Below are a number of common uses for interceptors.</p>\n<h3 id=\"setting-default-headers\">Setting default headers<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#setting-default-headers\"><i class=\"material-icons\">link</i></a></h3>\n<p>Apps often use an interceptor to set default headers on outgoing requests.</p>\n<p>The sample app has an <code>AuthService</code> that produces an authorization token.\nHere is its <code>AuthInterceptor</code> that injects that service to get the token and\nadds an authorization header with that token to every outgoing request:</p>\n<code-example path=\"http/src/app/http-interceptors/auth-interceptor.ts\" header=\"app/http-interceptors/auth-interceptor.ts\">\nimport { AuthService } from '../auth.service';\n\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()\nexport class AuthInterceptor implements <a href=\"api/common/http/HttpInterceptor\" class=\"code-anchor\">HttpInterceptor</a> {\n\n constructor(private auth: AuthService) {}\n\n intercept(req: <a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a><any>, next: <a href=\"api/common/http/HttpHandler\" class=\"code-anchor\">HttpHandler</a>) {\n // Get the auth token from the service.\n const authToken = this.auth.getAuthorizationToken();\n\n // Clone the request and replace the original headers with\n // cloned headers, updated with the authorization.\n const authReq = req.clone({\n headers: req.headers.set('Authorization', authToken)\n });\n\n // send cloned request with header to the next handler.\n return next.handle(authReq);\n }\n}\n\n</code-example>\n<p>The practice of cloning a request to set new headers is so common that\nthere's a <code>setHeaders</code> shortcut for it:</p>\n<code-example path=\"http/src/app/http-interceptors/auth-interceptor.ts\" region=\"set-header-shortcut\">\n// Clone the request and set the new header in one step.\nconst authReq = req.clone({ setHeaders: { Authorization: authToken } });\n\n</code-example>\n<p>An interceptor that alters headers can be used for a number of different operations, including:</p>\n<ul>\n<li>Authentication/authorization</li>\n<li>Caching behavior; for example, <code>If-Modified-Since</code></li>\n<li>XSRF protection</li>\n</ul>\n<h3 id=\"logging-request-and-response-pairs\">Logging request and response pairs<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#logging-request-and-response-pairs\"><i class=\"material-icons\">link</i></a></h3>\n<p>Because interceptors can process the request and response <em>together</em>, they can perform tasks such as timing and logging an entire HTTP operation.</p>\n<p>Consider the following <code>LoggingInterceptor</code>, which captures the time of the request,\nthe time of the response, and logs the outcome with the elapsed time\nwith the injected <code>MessageService</code>.</p>\n<code-example path=\"http/src/app/http-interceptors/logging-interceptor.ts\" region=\"excerpt\" header=\"app/http-interceptors/logging-interceptor.ts)\">\nimport { finalize, tap } from 'rxjs/operators';\nimport { MessageService } from '../message.service';\n\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()\nexport class LoggingInterceptor implements <a href=\"api/common/http/HttpInterceptor\" class=\"code-anchor\">HttpInterceptor</a> {\n constructor(private messenger: MessageService) {}\n\n intercept(req: <a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a><any>, next: <a href=\"api/common/http/HttpHandler\" class=\"code-anchor\">HttpHandler</a>) {\n const started = Date.now();\n let ok: string;\n\n // extend server response observable with logging\n return next.handle(req)\n .pipe(\n tap(\n // Succeeds when there is a response; ignore other events\n event => ok = event instanceof <a href=\"api/common/http/HttpResponse\" class=\"code-anchor\">HttpResponse</a> ? 'succeeded' : '',\n // Operation failed; error is an <a href=\"api/common/http/HttpErrorResponse\" class=\"code-anchor\">HttpErrorResponse</a>\n error => ok = 'failed'\n ),\n // Log when response observable either completes or errors\n finalize(() => {\n const elapsed = Date.now() - started;\n const msg = `${req.method} \"${req.urlWithParams}\"\n ${ok} in ${elapsed} ms.`;\n this.messenger.add(msg);\n })\n );\n }\n}\n\n</code-example>\n<p>The RxJS <code>tap</code> operator captures whether the request succeeded or failed.\nThe RxJS <code>finalize</code> operator is called when the response observable either errors or completes (which it must),\nand reports the outcome to the <code>MessageService</code>.</p>\n<p>Neither <code>tap</code> nor <code>finalize</code> touch the values of the observable stream returned to the caller.</p>\n<a id=\"custom-json-parser\"></a>\n<h3 id=\"custom-json-parsing\">Custom JSON parsing<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#custom-json-parsing\"><i class=\"material-icons\">link</i></a></h3>\n<p>Interceptors can be used to replace the built-in JSON parsing with a custom implementation.</p>\n<p>The <code>CustomJsonInterceptor</code> in the following example demonstrates how to achieve this.\nIf the intercepted request expects a <code>'json'</code> response, the <code>reponseType</code> is changed to <code>'text'</code>\nto disable the built-in JSON parsing. Then the response is parsed via the injected <code>JsonParser</code>.</p>\n<code-example path=\"http/src/app/http-interceptors/custom-json-interceptor.ts\" region=\"custom-json-interceptor\" header=\"app/http-interceptors/custom-json-interceptor.ts\">\n// The JsonParser class acts as a base class for custom parsers and as the DI token.\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()\nexport abstract class JsonParser {\n abstract parse(text: string): any;\n}\n\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()\nexport class CustomJsonInterceptor implements <a href=\"api/common/http/HttpInterceptor\" class=\"code-anchor\">HttpInterceptor</a> {\n constructor(private jsonParser: JsonParser) {}\n\n intercept(httpRequest: <a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a><any>, next: <a href=\"api/common/http/HttpHandler\" class=\"code-anchor\">HttpHandler</a>) {\n if (httpRequest.responseType === 'json') {\n // If the expected response type is JSON then handle it here.\n return this.handleJsonResponse(httpRequest, next);\n } else {\n return next.handle(httpRequest);\n }\n }\n\n private handleJsonResponse(httpRequest: <a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a><any>, next: <a href=\"api/common/http/HttpHandler\" class=\"code-anchor\">HttpHandler</a>) {\n // Override the responseType to disable the default JSON parsing.\n httpRequest = httpRequest.clone({responseType: 'text'});\n // Handle the response using the custom parser.\n return next.handle(httpRequest).pipe(map(event => this.parseJsonResponse(event)));\n }\n\n private parseJsonResponse(event: <a href=\"api/common/http/HttpEvent\" class=\"code-anchor\">HttpEvent</a><any>) {\n if (event instanceof <a href=\"api/common/http/HttpResponse\" class=\"code-anchor\">HttpResponse</a> && typeof event.body === 'string') {\n return event.clone({body: this.jsonParser.parse(event.body)});\n } else {\n return event;\n }\n }\n}\n\n</code-example>\n<p>You can then implement your own custom <code>JsonParser</code>.\nHere is a custom JsonParser that has a special date reviver.</p>\n<code-example path=\"http/src/app/http-interceptors/custom-json-interceptor.ts\" region=\"custom-json-parser\" header=\"app/http-interceptors/custom-json-interceptor.ts\">\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()\nexport class CustomJsonParser implements JsonParser {\n parse(text: string): any {\n return JSON.parse(text, dateReviver);\n }\n}\n\nfunction dateReviver(key: string, value: any) {\n/* . . . */\n}\n\n</code-example>\n<p>You provide the <code>CustomParser</code> along with the <code>CustomJsonInterceptor</code>.</p>\n<code-example path=\"http/src/app/http-interceptors/index.ts\" region=\"custom-json-interceptor\" header=\"app/http-interceptors/index.ts\">\n{ provide: <a href=\"api/common/http/HTTP_INTERCEPTORS\" class=\"code-anchor\">HTTP_INTERCEPTORS</a>, useClass: CustomJsonInterceptor, multi: true },\n{ provide: JsonParser, useClass: CustomJsonParser },\n\n</code-example>\n<a id=\"caching\"></a>\n<h3 id=\"caching-requests\">Caching requests<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#caching-requests\"><i class=\"material-icons\">link</i></a></h3>\n<p>Interceptors can handle requests by themselves, without forwarding to <code>next.handle()</code>.</p>\n<p>For example, you might decide to cache certain requests and responses to improve performance.\nYou can delegate caching to an interceptor without disturbing your existing data services.</p>\n<p>The <code>CachingInterceptor</code> in the following example demonstrates this approach.</p>\n<code-example path=\"http/src/app/http-interceptors/caching-interceptor.ts\" region=\"v1\" header=\"app/http-interceptors/caching-interceptor.ts)\">\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()\nexport class CachingInterceptor implements <a href=\"api/common/http/HttpInterceptor\" class=\"code-anchor\">HttpInterceptor</a> {\n constructor(private cache: RequestCache) {}\n\n intercept(req: <a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a><any>, next: <a href=\"api/common/http/HttpHandler\" class=\"code-anchor\">HttpHandler</a>) {\n // continue if not cacheable.\n if (!isCacheable(req)) { return next.handle(req); }\n\n const cachedResponse = this.cache.get(req);\n return cachedResponse ?\n of(cachedResponse) : sendRequest(req, next, this.cache);\n }\n}\n\n</code-example>\n<ul>\n<li>\n<p>The <code>isCacheable()</code> function determines if the request is cacheable.\nIn this sample, only GET requests to the npm package search api are cacheable.</p>\n</li>\n<li>\n<p>If the request is not cacheable, the interceptor simply forwards the request\nto the next handler in the chain.</p>\n</li>\n<li>\n<p>If a cacheable request is found in the cache, the interceptor returns an <code>of()</code> <em>observable</em> with\nthe cached response, by-passing the <code>next</code> handler (and all other interceptors downstream).</p>\n</li>\n<li>\n<p>If a cacheable request is not in cache, the code calls <code>sendRequest()</code>.\nThis function creates a <a href=\"guide/http#immutability\">request clone</a> without headers, because the npm API forbids them.\nThe function then forwards the clone of the request to <code>next.handle()</code> which ultimately calls the server and returns the server's response.</p>\n</li>\n</ul>\n<a id=\"send-request\"></a>\n<code-example path=\"http/src/app/http-interceptors/caching-interceptor.ts\" region=\"send-request\">\n/**\n * Get server response observable by sending request to `next()`.\n * Will add the response to the cache on the way out.\n */\nfunction sendRequest(\n req: <a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a><any>,\n next: <a href=\"api/common/http/HttpHandler\" class=\"code-anchor\">HttpHandler</a>,\n cache: RequestCache): Observable<<a href=\"api/common/http/HttpEvent\" class=\"code-anchor\">HttpEvent</a><any>> {\n\n // No headers allowed in npm search request\n const noHeaderReq = req.clone({ headers: new <a href=\"api/common/http/HttpHeaders\" class=\"code-anchor\">HttpHeaders</a>() });\n\n return next.handle(noHeaderReq).pipe(\n tap(event => {\n // There may be other events besides the response.\n if (event instanceof <a href=\"api/common/http/HttpResponse\" class=\"code-anchor\">HttpResponse</a>) {\n cache.put(req, event); // Update the cache.\n }\n })\n );\n}\n\n</code-example>\n<p>Note how <code>sendRequest()</code> intercepts the response on its way back to the application.\nThis method pipes the response through the <code>tap()</code> operator, whose callback adds the response to the cache.</p>\n<p>The original response continues untouched back up through the chain of interceptors\nto the application caller.</p>\n<p>Data services, such as <code>PackageSearchService</code>, are unaware that\nsome of their <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> requests actually return cached responses.</p>\n<a id=\"cache-refresh\"></a>\n<h3 id=\"using-interceptors-to-request-multiple-values\">Using interceptors to request multiple values<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#using-interceptors-to-request-multiple-values\"><i class=\"material-icons\">link</i></a></h3>\n<p>The <code><a href=\"api/common/http/HttpClient#get\" class=\"code-anchor\">HttpClient.get()</a></code> method normally returns an observable that emits a single value, either the data or an error.\nAn interceptor can change this to an observable that emits <a href=\"guide/observables\">multiple values</a>.</p>\n<p>The following revised version of the <code>CachingInterceptor</code> optionally returns an observable that\nimmediately emits the cached response, sends the request on to the npm web API,\nand emits again later with the updated search results.</p>\n<code-example path=\"http/src/app/http-interceptors/caching-interceptor.ts\" region=\"intercept-refresh\">\n// cache-then-refresh\nif (req.headers.get('x-refresh')) {\n const results$ = sendRequest(req, next, this.cache);\n return cachedResponse ?\n results$.pipe( startWith(cachedResponse) ) :\n results$;\n}\n// cache-or-fetch\nreturn cachedResponse ?\n of(cachedResponse) : sendRequest(req, next, this.cache);\n\n</code-example>\n<div class=\"alert is-helpful\">\n<p>The <em>cache-then-refresh</em> option is triggered by the presence of a custom <code>x-refresh</code> header.</p>\n<p>A checkbox on the <code>PackageSearchComponent</code> toggles a <code>withRefresh</code> flag,\nwhich is one of the arguments to <code>PackageSearchService.search()</code>.\nThat <code>search()</code> method creates the custom <code>x-refresh</code> header\nand adds it to the request before calling <code><a href=\"api/common/http/HttpClient#get\" class=\"code-anchor\">HttpClient.get()</a></code>.</p>\n</div>\n<p>The revised <code>CachingInterceptor</code> sets up a server request\nwhether there's a cached value or not,\nusing the same <code>sendRequest()</code> method described <a href=\"guide/http#send-request\">above</a>.\nThe <code>results$</code> observable makes the request when subscribed.</p>\n<ul>\n<li>\n<p>If there's no cached value, the interceptor returns <code>results$</code>.</p>\n</li>\n<li>\n<p>If there is a cached value, the code <em>pipes</em> the cached response onto\n<code>results$</code>, producing a recomposed observable that emits twice,\nthe cached response first (and immediately), followed later\nby the response from the server.\nSubscribers see a sequence of two responses.</p>\n</li>\n</ul>\n<a id=\"report-progress\"></a>\n<h2 id=\"tracking-and-showing-request-progress\">Tracking and showing request progress<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#tracking-and-showing-request-progress\"><i class=\"material-icons\">link</i></a></h2>\n<p>Sometimes applications transfer large amounts of data and those transfers can take a long time.\nFile uploads are a typical example.\nYou can give the users a better experience by providing feedback on the progress of such transfers.</p>\n<p>To make a request with progress events enabled, you can create an instance of <code><a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a></code>\nwith the <code>reportProgress</code> option set true to enable tracking of progress events.</p>\n<code-example path=\"http/src/app/uploader/uploader.service.ts\" region=\"upload-request\" header=\"app/uploader/uploader.service.ts (upload request)\">\nconst req = new <a href=\"api/common/http/HttpRequest\" class=\"code-anchor\">HttpRequest</a>('POST', '/upload/file', file, {\n reportProgress: true\n});\n\n</code-example>\n<div class=\"alert is-important\">\n<p><strong>Tip</strong>: Every progress event triggers change detection, so only turn them on if you need to report progress in the UI.</p>\n<p>When using <a href=\"api/common/http/HttpClient#request\"><code>HttpClient.request()</code></a> with an HTTP method, configure the method with\n<a href=\"api/common/http/HttpClient#request\"><code>observe: 'events'</code></a> to see all events, including the progress of transfers.</p>\n</div>\n<p>Next, pass this request object to the <code><a href=\"api/common/http/HttpClient#request\" class=\"code-anchor\">HttpClient.request()</a></code> method, which\nreturns an <code>Observable</code> of <code>HttpEvents</code> (the same events processed by <a href=\"guide/http#interceptor-events\">interceptors</a>).</p>\n<code-example path=\"http/src/app/uploader/uploader.service.ts\" region=\"upload-body\" header=\"app/uploader/uploader.service.ts (upload body)\">\n// The `HttpClient.request` API produces a raw event stream\n// which includes start (sent), progress, and response events.\nreturn this.http.request(req).pipe(\n map(event => this.getEventMessage(event, file)),\n tap(message => this.showProgress(message)),\n last(), // return last (completed) message to caller\n catchError(this.handleError(file))\n);\n\n</code-example>\n<p>The <code>getEventMessage</code> method interprets each type of <code><a href=\"api/common/http/HttpEvent\" class=\"code-anchor\">HttpEvent</a></code> in the event stream.</p>\n<code-example path=\"http/src/app/uploader/uploader.service.ts\" region=\"getEventMessage\" header=\"app/uploader/uploader.service.ts (getEventMessage)\">\n/** Return distinct message for sent, upload progress, & response events */\nprivate getEventMessage(event: <a href=\"api/common/http/HttpEvent\" class=\"code-anchor\">HttpEvent</a><any>, file: File) {\n switch (event.type) {\n case <a href=\"api/common/http/HttpEventType#Sent\" class=\"code-anchor\">HttpEventType.Sent</a>:\n return `Uploading file \"${file.name}\" of size ${file.size}.`;\n\n case <a href=\"api/common/http/HttpEventType#UploadProgress\" class=\"code-anchor\">HttpEventType.UploadProgress</a>:\n // Compute and show the % done:\n const percentDone = Math.round(100 * event.loaded / event.total);\n return `File \"${file.name}\" is ${percentDone}% uploaded.`;\n\n case <a href=\"api/common/http/HttpEventType#Response\" class=\"code-anchor\">HttpEventType.Response</a>:\n return `File \"${file.name}\" was completely uploaded!`;\n\n default:\n return `File \"${file.name}\" surprising upload event: ${event.type}.`;\n }\n}\n\n</code-example>\n<div class=\"alert is-helpful\">\n<p>The sample app for this guide doesn't have a server that accepts uploaded files.\nThe <code>UploadInterceptor</code> in <code>app/http-interceptors/upload-interceptor.ts</code>\nintercepts and short-circuits upload requests\nby returning an observable of simulated events.</p>\n</div>\n<h2 id=\"optimizing-server-interaction-with-debouncing\">Optimizing server interaction with debouncing<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#optimizing-server-interaction-with-debouncing\"><i class=\"material-icons\">link</i></a></h2>\n<p>If you need to make an HTTP request in response to user input, it's not efficient to send a request for every keystroke.\nIt's better to wait until the user stops typing and then send a request.\nThis technique is known as debouncing.</p>\n<p>Consider the following template, which lets a user enter a search term to find an npm package by name.\nWhen the user enters a name in a search-box, the <code>PackageSearchComponent</code> sends\na search request for a package with that name to the npm web API.</p>\n<code-example path=\"http/src/app/package-search/package-search.component.html\" region=\"search\" header=\"app/package-search/package-search.component.html (search)\">\n<input (keyup)=\"search(getValue($event.target))\" id=\"name\" placeholder=\"Search\"/>\n\n<ul>\n <li *<a href=\"api/common/NgForOf\" class=\"code-anchor\">ngFor</a>=\"let package of packages$ | async\">\n <b>{{package.name}} v.{{package.version}}</b> -\n <i>{{package.description}}</i>\n </li>\n</ul>\n\n</code-example>\n<p>Here, the <code>keyup</code> event binding sends every keystroke to the component's <code>search()</code> method.</p>\n<div class=\"alert is-helpful\">\n<p>The type of <code>$event.target</code> is only <code>EventTarget</code> in the template.\nIn the <code>getValue()</code> method, the target is cast to an <code>HTMLInputElement</code> to allow type-safe access to its <code>value</code> property.</p>\n<code-example path=\"http/src/app/package-search/package-search.component.ts\" region=\"getValue\">\ngetValue(target: EventTarget): string {\n return (target as HTMLInputElement).value;\n}\n\n</code-example>\n</div>\n<p>The following snippet implements debouncing for this input using RxJS operators.</p>\n<code-example path=\"http/src/app/package-search/package-search.component.ts\" region=\"debounce\" header=\"app/package-search/package-search.component.ts (excerpt)\">\nwithRefresh = false;\npackages$: Observable<NpmPackageInfo[]>;\nprivate searchText$ = new Subject<string>();\n\nsearch(packageName: string) {\n this.searchText$.next(packageName);\n}\n\nngOnInit() {\n this.packages$ = this.searchText$.pipe(\n debounceTime(500),\n distinctUntilChanged(),\n switchMap(packageName =>\n this.searchService.search(packageName, this.withRefresh))\n );\n}\n\nconstructor(private searchService: PackageSearchService) { }\n\n\n</code-example>\n<p>The <code>searchText$</code> is the sequence of search-box values coming from the user.\nIt's defined as an RxJS <code>Subject</code>, which means it is a multicasting <code>Observable</code>\nthat can also emit values for itself by calling <code>next(value)</code>,\nas happens in the <code>search()</code> method.</p>\n<p>Rather than forward every <code>searchText</code> value directly to the injected <code>PackageSearchService</code>,\nthe code in <code>ngOnInit()</code> pipes search values through three operators, so that a search value reaches the service only if it's a new value and the user has stopped typing.</p>\n<ul>\n<li>\n<p><code>debounceTime(500)</code>—Wait for the user to stop typing (1/2 second in this case).</p>\n</li>\n<li>\n<p><code>distinctUntilChanged()</code>—Wait until the search text changes.</p>\n</li>\n<li>\n<p><code>switchMap()</code>—Send the search request to the service.</p>\n</li>\n</ul>\n<p>The code sets <code>packages$</code> to this re-composed <code>Observable</code> of search results.\nThe template subscribes to <code>packages$</code> with the <a href=\"api/common/AsyncPipe\">AsyncPipe</a>\nand displays search results as they arrive.</p>\n<div class=\"alert is-helpful\">\n<p>See <a href=\"guide/http#cache-refresh\">Using interceptors to request multiple values</a> for more about the <code>withRefresh</code> option.</p>\n</div>\n<h3 id=\"using-the-switchmap-operator\">Using the <em>switchMap()</em> operator<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#using-the-switchmap-operator\"><i class=\"material-icons\">link</i></a></h3>\n<p>The <code>switchMap()</code> operator takes a function argument that returns an <code>Observable</code>.\nIn the example, <code>PackageSearchService.search</code> returns an <code>Observable</code>, as other data service methods do.\nIf a previous search request is still in-flight (as when the network connection is poor),\nthe operator cancels that request and sends a new one.</p>\n<p>Note that <code>switchMap()</code> returns service responses in their original request order, even if the\nserver returns them out of order.</p>\n<div class=\"alert is-helpful\">\n<p>If you think you'll reuse this debouncing logic,\nconsider moving it to a utility function or into the <code>PackageSearchService</code> itself.</p>\n</div>\n<h2 id=\"security-xsrf-protection\">Security: XSRF protection<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#security-xsrf-protection\"><i class=\"material-icons\">link</i></a></h2>\n<p><a href=\"https://en.wikipedia.org/wiki/Cross-site_request_forgery\">Cross-Site Request Forgery (XSRF or CSRF)</a> is an attack technique by which the attacker can trick an authenticated user into unknowingly executing actions on your website.\n<code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> supports a <a href=\"https://en.wikipedia.org/wiki/Cross-site_request_forgery#Cookie-to-header_token\">common mechanism</a> used to prevent XSRF attacks.\nWhen performing HTTP requests, an interceptor reads a token from a cookie, by default <code>XSRF-TOKEN</code>, and sets it as an HTTP header, <code>X-XSRF-TOKEN</code>.\nSince only code that runs on your domain could read the cookie, the backend can be certain that the HTTP request came from your client application and not an attacker.</p>\n<p>By default, an interceptor sends this header on all mutating requests (such as POST)\nto relative URLs, but not on GET/HEAD requests or on requests with an absolute URL.</p>\n<p>To take advantage of this, your server needs to set a token in a JavaScript readable session cookie called <code>XSRF-TOKEN</code> on either the page load or the first GET request.\nOn subsequent requests the server can verify that the cookie matches the <code>X-XSRF-TOKEN</code> HTTP header, and therefore be sure that only code running on your domain could have sent the request.\nThe token must be unique for each user and must be verifiable by the server; this prevents the client from making up its own tokens.\nSet the token to a digest of your site's authentication cookie with a salt for added security.</p>\n<p>In order to prevent collisions in environments where multiple Angular apps share the same domain or subdomain, give each application a unique cookie name.</p>\n<div class=\"alert is-important\">\n<p><em><code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> supports only the client half of the XSRF protection scheme.</em>\nYour backend service must be configured to set the cookie for your page, and to verify that\nthe header is present on all eligible requests.\nFailing to do so renders Angular's default protection ineffective.</p>\n</div>\n<h3 id=\"configuring-custom-cookieheader-names\">Configuring custom cookie/header names<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#configuring-custom-cookieheader-names\"><i class=\"material-icons\">link</i></a></h3>\n<p>If your backend service uses different names for the XSRF token cookie or header,\nuse <code><a href=\"api/common/http/HttpClientXsrfModule#withOptions\" class=\"code-anchor\">HttpClientXsrfModule.withOptions()</a></code> to override the defaults.</p>\n<code-example path=\"http/src/app/app.module.ts\" region=\"xsrf\">\nimports: [\n <a href=\"api/common/http/HttpClientModule\" class=\"code-anchor\">HttpClientModule</a>,\n HttpClientXsrfModule.withOptions({\n cookieName: 'My-Xsrf-Cookie',\n headerName: 'My-Xsrf-Header',\n }),\n],\n\n</code-example>\n<a id=\"testing-requests\"></a>\n<h2 id=\"testing-http-requests\">Testing HTTP requests<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#testing-http-requests\"><i class=\"material-icons\">link</i></a></h2>\n<p>As for any external dependency, you must mock the HTTP backend so your tests can simulate interaction with a remote server.\nThe <code>@angular/common/<a href=\"api/common/http\" class=\"code-anchor\">http</a>/testing</code> library makes it straightforward to set up such mocking.</p>\n<p>Angular's HTTP testing library is designed for a pattern of testing in which the app executes code and makes requests first.\nThe test then expects that certain requests have or have not been made,\nperforms assertions against those requests,\nand finally provides responses by \"flushing\" each expected request.</p>\n<p>At the end, tests can verify that the app has made no unexpected requests.</p>\n<div class=\"alert is-helpful\">\n<p>You can run <live-example stackblitz=\"specs\">these sample tests</live-example>\nin a live coding environment.</p>\n<p>The tests described in this guide are in <code>src/testing/http-client.spec.ts</code>.\nThere are also tests of an application data service that call <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> in\n<code>src/app/heroes/heroes.service.spec.ts</code>.</p>\n</div>\n<h3 id=\"setup-for-testing\">Setup for testing<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#setup-for-testing\"><i class=\"material-icons\">link</i></a></h3>\n<p>To begin testing calls to <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code>,\nimport the <code><a href=\"api/common/http/testing/HttpClientTestingModule\" class=\"code-anchor\">HttpClientTestingModule</a></code> and the mocking controller, <code><a href=\"api/common/http/testing/HttpTestingController\" class=\"code-anchor\">HttpTestingController</a></code>,\nalong with the other symbols your tests require.</p>\n<code-example path=\"http/src/testing/http-client.spec.ts\" region=\"imports\" header=\"app/testing/http-client.spec.ts (imports)\">\n// Http testing module and mocking controller\nimport { <a href=\"api/common/http/testing/HttpClientTestingModule\" class=\"code-anchor\">HttpClientTestingModule</a>, <a href=\"api/common/http/testing/HttpTestingController\" class=\"code-anchor\">HttpTestingController</a> } from '@angular/common/<a href=\"api/common/http\" class=\"code-anchor\">http</a>/testing';\n\n// Other imports\nimport { <a href=\"api/core/testing/TestBed\" class=\"code-anchor\">TestBed</a> } from '@angular/core/testing';\nimport { <a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a>, <a href=\"api/common/http/HttpErrorResponse\" class=\"code-anchor\">HttpErrorResponse</a> } from '@angular/common/<a href=\"api/common/http\" class=\"code-anchor\">http</a>';\n\n\n</code-example>\n<p>Then add the <code><a href=\"api/common/http/testing/HttpClientTestingModule\" class=\"code-anchor\">HttpClientTestingModule</a></code> to the <code><a href=\"api/core/testing/TestBed\" class=\"code-anchor\">TestBed</a></code> and continue with\nthe setup of the <em>service-under-test</em>.</p>\n<code-example path=\"http/src/testing/http-client.spec.ts\" region=\"setup\" header=\"app/testing/http-client.spec.ts(setup)\">\ndescribe('<a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a> testing', () => {\n let httpClient: <a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a>;\n let httpTestingController: <a href=\"api/common/http/testing/HttpTestingController\" class=\"code-anchor\">HttpTestingController</a>;\n\n beforeEach(() => {\n TestBed.configureTestingModule({\n imports: [ <a href=\"api/common/http/testing/HttpClientTestingModule\" class=\"code-anchor\">HttpClientTestingModule</a> ]\n });\n\n // <a href=\"api/core/Inject\" class=\"code-anchor\">Inject</a> the <a href=\"api/common/http\" class=\"code-anchor\">http</a> service and test controller for each test\n httpClient = TestBed.inject(<a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a>);\n httpTestingController = TestBed.inject(<a href=\"api/common/http/testing/HttpTestingController\" class=\"code-anchor\">HttpTestingController</a>);\n });\n /// Tests begin ///\n});\n\n</code-example>\n<p>Now requests made in the course of your tests hit the testing backend instead of the normal backend.</p>\n<p>This setup also calls <code>TestBed.inject()</code> to inject the <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> service and the mocking controller\nso they can be referenced during the tests.</p>\n<h3 id=\"expecting-and-answering-requests\">Expecting and answering requests<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#expecting-and-answering-requests\"><i class=\"material-icons\">link</i></a></h3>\n<p>Now you can write a test that expects a GET Request to occur and provides a mock response.</p>\n<code-example path=\"http/src/testing/http-client.spec.ts\" region=\"get-test\" header=\"app/testing/http-client.spec.ts (HttpClient.get)\">\nit('can test HttpClient.get', () => {\n const testData: <a href=\"api/router/Data\" class=\"code-anchor\">Data</a> = {name: 'Test <a href=\"api/router/Data\" class=\"code-anchor\">Data</a>'};\n\n // Make an HTTP GET request\n httpClient.get<<a href=\"api/router/Data\" class=\"code-anchor\">Data</a>>(testUrl)\n .subscribe(data =>\n // When observable resolves, result should match test data\n expect(data).toEqual(testData)\n );\n\n // The following `expectOne()` will match the request's URL.\n // If no requests or <a href=\"api/forms/SelectMultipleControlValueAccessor\" class=\"code-anchor\">multiple</a> requests matched that URL\n // `expectOne()` would throw.\n const req = httpTestingController.expectOne('/data');\n\n // Assert that the request is a GET.\n expect(req.request.method).toEqual('GET');\n\n // Respond with mock data, causing Observable to resolve.\n // Subscribe callback asserts that correct data was returned.\n req.flush(testData);\n\n // Finally, assert that there are no outstanding requests.\n httpTestingController.verify();\n});\n\n</code-example>\n<p>The last step, verifying that no requests remain outstanding, is common enough for you to move it into an <code>afterEach()</code> step:</p>\n<code-example path=\"http/src/testing/http-client.spec.ts\" region=\"afterEach\">\nafterEach(() => {\n // After every test, assert that there are no more pending requests.\n httpTestingController.verify();\n});\n\n</code-example>\n<h4 id=\"custom-request-expectations\">Custom request expectations<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#custom-request-expectations\"><i class=\"material-icons\">link</i></a></h4>\n<p>If matching by URL isn't sufficient, it's possible to implement your own matching function.\nFor example, you could look for an outgoing request that has an authorization header:</p>\n<code-example path=\"http/src/testing/http-client.spec.ts\" region=\"predicate\">\n// Expect one request with an authorization header\nconst req = httpTestingController.expectOne(\n request => request.headers.has('Authorization')\n);\n\n</code-example>\n<p>As with the previous <code>expectOne()</code>,\nthe test fails if 0 or 2+ requests satisfy this predicate.</p>\n<h4 id=\"handling-more-than-one-request\">Handling more than one request<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#handling-more-than-one-request\"><i class=\"material-icons\">link</i></a></h4>\n<p>If you need to respond to duplicate requests in your test, use the <code>match()</code> API instead of <code>expectOne()</code>.\nIt takes the same arguments but returns an array of matching requests.\nOnce returned, these requests are removed from future matching and\nyou are responsible for flushing and verifying them.</p>\n<code-example path=\"http/src/testing/http-client.spec.ts\" region=\"multi-request\">\n// get all pending requests that match the given URL\nconst requests = httpTestingController.match(testUrl);\nexpect(requests.length).toEqual(3);\n\n// Respond to each request with different results\nrequests[0].flush([]);\nrequests[1].flush([testData[0]]);\nrequests[2].flush(testData);\n\n</code-example>\n<h3 id=\"testing-for-errors\">Testing for errors<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/http#testing-for-errors\"><i class=\"material-icons\">link</i></a></h3>\n<p>You should test the app's defenses against HTTP requests that fail.</p>\n<p>Call <code>request.flush()</code> with an error message, as seen in the following example.</p>\n<code-example path=\"http/src/testing/http-client.spec.ts\" region=\"404\">\nit('can test for 404 error', () => {\n const emsg = 'deliberate 404 error';\n\n httpClient.get<<a href=\"api/router/Data\" class=\"code-anchor\">Data</a>[]>(testUrl).subscribe(\n data => fail('should have failed with the 404 error'),\n (error: <a href=\"api/common/http/HttpErrorResponse\" class=\"code-anchor\">HttpErrorResponse</a>) => {\n expect(error.status).toEqual(404, 'status');\n expect(error.error).toEqual(emsg, 'message');\n }\n );\n\n const req = httpTestingController.expectOne(testUrl);\n\n // Respond with mock error\n req.flush(emsg, { status: 404, statusText: 'Not Found' });\n});\n\n</code-example>\n<p>Alternatively, you can call <code>request.error()</code> with an <code>ErrorEvent</code>.</p>\n<code-example path=\"http/src/testing/http-client.spec.ts\" region=\"network-error\">\nit('can test for network error', () => {\n const emsg = 'simulated network error';\n\n httpClient.get<<a href=\"api/router/Data\" class=\"code-anchor\">Data</a>[]>(testUrl).subscribe(\n data => fail('should have failed with the network error'),\n (error: <a href=\"api/common/http/HttpErrorResponse\" class=\"code-anchor\">HttpErrorResponse</a>) => {\n expect(error.error.message).toEqual(emsg, 'message');\n }\n );\n\n const req = httpTestingController.expectOne(testUrl);\n\n // Create mock ErrorEvent, raised when something goes wrong at the network level.\n // Connection timeout, DNS error, offline, etc\n const mockError = new ErrorEvent('Network error', {\n message: emsg,\n });\n\n // Respond with mock error\n req.error(mockError);\n});\n\n</code-example>\n\n \n</div>\n\n<!-- links to this doc:\n - api/common/http\n - api/common/http/HttpClient\n - api/common/http/HttpInterceptor\n - guide/architecture-next-steps\n - guide/docs-style-guide\n - guide/example-apps-list\n - guide/npm-packages\n - guide/testing-components-scenarios\n - guide/testing-services\n - guide/what-is-angular\n - start/start-data\n - tutorial/toh-pt4\n-->\n<!-- links from this doc:\n - api/common/AsyncPipe\n - api/common/DecimalPipe\n - api/common/NgForOf\n - api/common/http\n - api/common/http/HTTP_INTERCEPTORS\n - api/common/http/HttpBackend\n - api/common/http/HttpClient\n - api/common/http/HttpClient#get\n - api/common/http/HttpClient#post\n - api/common/http/HttpClient#put\n - api/common/http/HttpClient#request\n - api/common/http/HttpClientJsonpModule\n - api/common/http/HttpClientModule\n - api/common/http/HttpClientXsrfModule#withOptions\n - api/common/http/HttpErrorResponse\n - api/common/http/HttpEvent\n - api/common/http/HttpEventType#Response\n - api/common/http/HttpEventType#Sent\n - api/common/http/HttpEventType#UploadProgress\n - api/common/http/HttpHandler\n - api/common/http/HttpHeaders\n - api/common/http/HttpInterceptor\n - api/common/http/HttpParams\n - api/common/http/HttpRequest\n - api/common/http/HttpResponse\n - api/common/http/testing/HttpClientTestingModule\n - api/common/http/testing/HttpTestingController\n - api/core/Inject\n - api/core/Injectable\n - api/core/NgModule\n - api/core/testing/TestBed\n - api/forms/SelectMultipleControlValueAccessor\n - api/platform-browser/BrowserModule\n - api/router/Data\n - guide/architecture\n - guide/dependency-injection\n - guide/glossary#observable\n - guide/glossary#service\n - guide/http#adding-and-updating-headers\n - guide/http#adding-headers\n - guide/http#always-subscribe\n - guide/http#cache-refresh\n - guide/http#caching\n - guide/http#caching-requests\n - guide/http#clearing-the-request-body-in-a-clone\n - guide/http#communicating-with-backend-services-using-http\n - guide/http#config-service\n - guide/http#configuring-custom-cookieheader-names\n - guide/http#configuring-http-url-parameters\n - guide/http#custom-json-parsing\n - guide/http#custom-request-expectations\n - guide/http#error-details\n - guide/http#error-handling\n - guide/http#expecting-and-answering-requests\n - guide/http#getting-error-details\n - guide/http#handling-interceptor-events\n - guide/http#handling-more-than-one-request\n - guide/http#handling-request-errors\n - guide/http#http-interceptor-use-cases\n - guide/http#immutability\n - guide/http#intercepting-requests-and-responses\n - guide/http#interceptor-events\n - guide/http#interceptor-order\n - guide/http#logging-request-and-response-pairs\n - guide/http#making-a-delete-request\n - guide/http#making-a-jsonp-request\n - guide/http#making-a-post-request\n - guide/http#making-a-put-request\n - guide/http#modifying-a-request-body\n - guide/http#optimizing-server-interaction-with-debouncing\n - guide/http#prerequisites\n - guide/http#provide-the-interceptor\n - guide/http#reading-the-full-response\n - guide/http#report-progress\n - guide/http#requesting-a-typed-response\n - guide/http#requesting-data-from-a-server\n - guide/http#requesting-non-json-data\n - guide/http#retry\n - guide/http#retrying-a-failed-request\n - guide/http#security-xsrf-protection\n - guide/http#send-request\n - guide/http#sending-data-to-a-server\n - guide/http#setting-default-headers\n - guide/http#setup-for-server-communication\n - guide/http#setup-for-testing\n - guide/http#testing-for-errors\n - guide/http#testing-http-requests\n - guide/http#testing-requests\n - guide/http#the-next-object\n - guide/http#tracking-and-showing-request-progress\n - guide/http#typed-response\n - guide/http#updating-headers\n - guide/http#url-params\n - guide/http#using-interceptors-to-request-multiple-values\n - guide/http#using-the-switchmap-operator\n - guide/http#write-an-interceptor\n - guide/observables\n - guide/pipes\n - guide/rx-library\n - guide/rx-library#operators\n - https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS\n - https://en.wikipedia.org/wiki/Cross-site_request_forgery\n - https://en.wikipedia.org/wiki/Cross-site_request_forgery#Cookie-to-header_token\n - https://en.wikipedia.org/wiki/JSONP\n - https://github.com/angular/angular/edit/master/aio/content/guide/http.md?message=docs%3A%20describe%20your%20change...\n - https://github.com/angular/angular/tree/master/packages/misc/angular-in-memory-web-api\n-->"
|
||
} |