2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# HttpClient
  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Most front-end applications communicate with backend services over the HTTP protocol. Modern browsers support two different APIs for making HTTP requests: the `XMLHttpRequest`  interface and the `fetch()`  API.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `HttpClient`  in `@angular/common/http`  offers a simplified client HTTP API for Angular applications
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								that rests on the `XMLHttpRequest`  interface exposed by browsers.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Additional benefits of `HttpClient`  include testability features, typed request and response objects, request and response interception, `Observable`  apis, and streamlined error handling.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can run the < live-example > < / live-example >  that accompanies this guide.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< div  class = "alert is-helpful" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The sample app does not require a data server.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								It relies on the 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[Angular _in-memory-web-api_ ](https://github.com/angular/in-memory-web-api/blob/master/README.md ),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								which replaces the _HttpClient_  module's `HttpBackend` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The replacement service simulates the behavior of a REST-like backend.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Look at the `AppModule`  _imports_  to see how it is configured.
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Setup
  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Before you can use the `HttpClient` , you need to import the Angular `HttpClientModule` . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Most apps do so in the root `AppModule` .
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/app.module.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="sketch"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/app.module.ts (excerpt)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Having imported `HttpClientModule`  into the `AppModule` , you can inject the `HttpClient` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								into an application class as shown in the following `ConfigService`  example.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="proto"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/config/config.service.ts (excerpt)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Getting JSON data
  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Applications often request JSON data from the server. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, the app might need a configuration file on the server, `config.json` , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								that specifies resource URLs.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/assets/config.json"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="assets/config.json" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-07-15 10:07:50 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `ConfigService`  fetches this file with a `get()`  method on `HttpClient` .
							 
						 
					
						
							
								
									
										
										
										
											2017-07-15 10:07:50 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="getConfig_1"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/config/config.service.ts (getConfig v.1)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-07-20 18:35:27 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A component, such as `ConfigComponent` , injects the `ConfigService`  and calls
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the `getConfig`  service method.
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.component.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="v1"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/config/config.component.ts (showConfig v.1)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Because the service method returns an `Observable`  of configuration data,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the component **subscribes**  to the method's return value.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The subscription callback copies the data fields into the component's `config`  object,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								which is data-bound in the component template for display.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Why write a service
  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This example is so simple that it is tempting to write the `Http.get()`  inside the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								component itself and skip the service.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								However, data access rarely stays this simple.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You typically post-process the data, add error handling, and maybe some retry logic to
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								cope with intermittent connectivity.
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The component quickly becomes cluttered with data access minutia.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The component becomes harder to understand, harder to test, and the data access logic can't be re-used or standardized.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								That's why it is a best practice to separate presentation of data from data access by
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								encapsulating data access in a separate service and delegating to that service in
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the component, even in simple cases like this one.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Type-checking the response
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The subscribe callback above requires bracket notation to extract the data values.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.component.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="v1_callback" linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can't write `data.heroesUrl`  because TypeScript correctly complains that the `data`  object from the service does not have a `heroesUrl`  property. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `HttpClient.get()`  method parsed the JSON server response into the anonymous `Object`  type. It doesn't know what the shape of that object is.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can tell `HttpClient`  the type of the response to make consuming the output easier and more obvious.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								First, define an interface with the correct shape:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="config-interface" linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Then, specify that interface as the `HttpClient.get()`  call's type parameter in the service:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="getConfig_2" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/config/config.service.ts (getConfig v.2)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The callback in the updated component method receives a typed data object, which is
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								easier and safer to consume:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.component.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="v2"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/config/config.component.ts (showConfig v.2)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Reading the full response
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The response body doesn't return all the data you may need. Sometimes servers return special headers or status codes to indicate certain conditions that are important to the application workflow. 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Tell `HttpClient`  that you want the full response with the `observe`  option:
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="getConfigResponse" linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Now `HttpClient.get()`  returns an `Observable`  of typed `HttpResponse`  rather than just the JSON data.
							 
						 
					
						
							
								
									
										
										
										
											2017-05-10 10:06:04 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The component's `showConfigResponse()`  method displays the response headers as well as the configuration:
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.component.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="showConfigResponse" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/config/config.component.ts (showConfigResponse)"
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								As you can see, the response object has a `body`  property of the correct type.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Error handling
  
						 
					
						
							
								
									
										
										
										
											2017-05-10 10:06:04 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								What happens if the request fails on the server, or if a poor network connection prevents it from even reaching the server? `HttpClient`  will return an _error_  object instead of a successful response.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You _could_  handle in the component by adding a second callback to the `.subscribe()` :
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.component.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="v3" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/config/config.component.ts (showConfig v.3 with error handling)"
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								It's certainly a good idea to give the user some kind of feedback when data access fails.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								But displaying the raw error object returned by `HttpClient`  is far from the best way to do it.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{@a  error-details}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Getting error details
  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Detecting that an error occurred is one thing.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Interpreting that error and composing a user-friendly response is a bit more involved.
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Two types of errors can occur. The server backend might reject the request, returning an HTTP response with a status code such as 404 or 500. These are error _responses_ .
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Or 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 `ErrorEvent`  objects.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `HttpClient`  captures both kinds of errors in its `HttpErrorResponse`  and you can inspect that response to figure out what really happened.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Error inspection, interpretation, and resolution is something you want to do in the _service_ , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								not in the _component_ .  
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You might first devise an error handler like this one:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="handleError" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/config/config.service.ts (handleError)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Notice that this handler returns an RxJS [`ErrorObservable` ](#rxjs ) with a user-friendly error message.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Consumers of the service expect service methods to return an `Observable`  of some kind,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								even a "bad" one.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Now you take the `Observables`  returned by the `HttpClient`  methods
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								and _pipe them through_  to the error handler.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="getConfig_3" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/config/config.service.ts (getConfig v.3 with error handler)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### `retry()`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Sometimes the error is transient and will go away automatically if you try again.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, network interruptions are common in mobile scenarios, and trying again
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								may produce a successful result.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The [RxJS library ](#rxjs ) offers several _retry_  operators that are worth exploring.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The simplest is called `retry()`  and it automatically re-subscribes to a failed `Observable`  a specified number of times. _Re-subscribing_  to the result of an `HttpClient`  method call has the effect of reissuing the HTTP request.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								_Pipe_ it onto the `HttpClient`  method result just before the error handler.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="getConfig" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/config/config.service.ts (getConfig with retry)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{@a  rxjs}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Observables and operators
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The previous sections of this guide referred to RxJS `Observables`  and operators such as `catchError`  and `retry` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You will encounter more RxJS artifacts as you continue below.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[RxJS ](http://reactivex.io/rxjs/ ) is a library for composing asynchronous and callback-based code
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								in a _functional, reactive style_ .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Many Angular APIs, including `HttpClient` , produce and consume RxJS `Observables` . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								RxJS itself is out-of-scope for this guide. You will find many learning resources on the web.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								While you can get by with a minimum of RxJS knowledge, you'll want to grow your RxJS skills over time in order to use `HttpClient`  effectively.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you're following along with these code snippets, note that you must import the RxJS observable and operator symbols that appear in those snippets. These `ConfigService`  imports are typical.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/config/config.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="rxjs-imports" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/config/config.service.ts (RxJS imports)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Requesting non-JSON data
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Not all APIs return JSON data. In this next example,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								a `DownloaderService`  method reads a text file from the server
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								and logs the file contents, before returning those contents to the caller
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								as an `Observable<string>` . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/downloader/downloader.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="getTextFile" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/downloader/downloader.service.ts (getTextFile)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`HttpClient.get()`  returns a string rather than the default JSON because of the `responseType`  option. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The RxJS `tap`  operator (as in "wiretap") lets the code inspect good and error values passing through the observable without disturbing them. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								A `download()`  method in the `DownloaderComponent`  initiates the request by subscribing to the service method.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/downloader/downloader.component.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="download" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/downloader/downloader.component.ts (download)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Sending data to the server
  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								In addition to fetching data from the server, `HttpClient`  supports mutating requests, that is, sending data to the server with other HTTP methods such as PUT, POST, and DELETE.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The sample app for this guide includes a simplified version of the "Tour of Heroes" example
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								that fetches heroes and enables users to add, delete, and update them.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following sections excerpt methods of the sample's `HeroesService` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Adding headers
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Many servers require extra headers for save operations.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, they may require a "Content-Type" header to explicitly declare 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the MIME type of the request body.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Or perhaps the server requires an authorization token.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `HeroesService`  defines such headers in an `httpOptions`  object that will be passed
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								to every `HttpClient`  save method.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/heroes/heroes.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="http-options" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/heroes/heroes.service.ts (httpOptions)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Making a POST request
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Apps often POST data to a server. They POST when submitting a form. 
							 
						 
					
						
							
								
									
										
										
										
											2018-03-23 11:22:21 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								In the following example, the `HeroesService`  posts when adding a hero to the database.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/heroes/heroes.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="addHero" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/heroes/heroes.service.ts (addHero)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `HttpClient.post()`  method is similar to `get()`  in that it has a type parameter
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								(you're expecting the server to return the new hero)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								and it takes a resource URL.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								It takes two more parameters:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1.  `hero`  - the data to POST in the body of the request. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1.  `httpOptions`  - the method options which, in this case, [specify required headers ](#adding-headers ). 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Of course it catches errors in much the same manner [described above ](#error-details ).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `HeroesComponent`  initiates the actual POST operation by subscribing to 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the `Observable`  returned by this service method.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/heroes/heroes.component.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="add-hero-subscribe" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/heroes/heroes.component.ts (addHero)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								When the server responds successfully with the newly added hero, the component adds
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								that hero to the displayed `heroes`  list.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Making a DELETE request
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This application deletes a hero with the `HttpClient.delete`  method by passing the hero's id
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								in the request URL.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/heroes/heroes.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="deleteHero" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/heroes/heroes.service.ts (deleteHero)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `HeroesComponent`  initiates the actual DELETE operation by subscribing to 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the `Observable`  returned by this service method.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/heroes/heroes.component.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="delete-hero-subscribe" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/heroes/heroes.component.ts (deleteHero)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-21 16:26:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								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 `subscribe()`  method _executes_  the observable, which is what initiates the DELETE request. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< div  class = "alert is-important" >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-23 11:22:21 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You must call _subscribe()_  or nothing happens. Just calling `HeroesService.deleteHero()`  **does not initiate the DELETE request.** 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/heroes/heroes.component.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="delete-hero-no-subscribe" linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{@a  always-subscribe}
							 
						 
					
						
							
								
									
										
										
										
											2018-03-21 16:26:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**Always _subscribe_ !**
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								An `HttpClient`  method does not begin its HTTP request until you call `subscribe()`  on the observable returned by that method. This is true for _all_  `HttpClient`  _methods_ .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  class = "alert is-helpful" >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The [`AsyncPipe` ](api/common/AsyncPipe ) subscribes (and unsubscribes) for you automatically.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								All observables returned from `HttpClient`  methods are _cold_  by design.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Execution of the HTTP request is _deferred_ , allowing you to extend the
							 
						 
					
						
							
								
									
										
										
										
											2018-03-21 16:26:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								observable with additional operations such as  `tap`  and `catchError`  before anything actually happens.
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Calling `subscribe(...)`  triggers execution of the observable and causes
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`HttpClient`  to compose and send the HTTP request to the server. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can think of these observables as _blueprints_  for actual HTTP requests.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< div  class = "alert is-helpful" >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In fact, each `subscribe()`  initiates a separate, independent execution of the observable.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Subscribing twice results in two HTTP requests.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const req = http.get< Heroes > ('/api/heroes');
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// 0 requests made - .subscribe() not called.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								req.subscribe();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// 1 request made.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								req.subscribe();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// 2 requests made.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Making a PUT request
  
						 
					
						
							
								
									
										
										
										
											2017-06-13 11:51:30 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								An app will send a PUT request to completely replace a resource with updated data.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-23 11:22:21 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The following `HeroesService`  example is just like the POST example.
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/heroes/heroes.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="updateHero" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/heroes/heroes.service.ts (updateHero)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For the reasons [explained above ](#always-subscribe ), the caller (`HeroesComponent.update()`  in this case) must `subscribe()`  to the observable returned from the `HttpClient.put()` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								in order to initiate the request.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Advanced usage
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-21 16:26:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								We have discussed the basic HTTP functionality in `@angular/common/http` , but sometimes you need to do more than make simple requests and get data back.
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Configuring the request
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Other aspects of an outgoing request can be configured via the options object
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								passed as the last argument to the `HttpClient`  method.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-23 11:22:21 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You [saw earlier ](#adding-headers ) that the `HeroesService`  sets the default headers by
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								passing an options object (`httpOptions` ) to its save methods.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can do more.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Update headers
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can't directly modify the existing headers within the previous options
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								object because instances of the `HttpHeaders`  class are immutable.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Use the `set()`  method instead. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								It returns a clone of the current instance with the new changes applied.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Here's how you might update the authorization header (after the old token expired) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								before making the next request.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/heroes/heroes.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="update-headers" linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### URL Parameters
  
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Adding URL search parameters works a similar way.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Here is a `searchHeroes`  method that queries for heroes whose names contain the search term.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/heroes/heroes.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="searchHeroes" linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-21 16:26:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If there is a search term, the code constructs an options object with an HTML URL-encoded search parameter. If the term were "foo", the GET request URL would be `api/heroes/?name=foo` .
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-05-03 11:54:46 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `HttpParams`  are immutable so you'll have to use the `set()`  method to update the options.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Debouncing requests
  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The sample includes an _npm package search_  feature.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								When the user enters a name in a search-box, the `PackageSearchComponent`  sends
							 
						 
					
						
							
								
									
										
										
										
											2018-03-21 16:26:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								a search request for a package with that name to the NPM web API.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Here's a pertinent excerpt from the template:
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/package-search/package-search.component.html"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="search" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/package-search/package-search.component.html (search)">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `(keyup)`  event binding sends every keystroke to the component's `search()`  method.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Sending a request for every keystroke could be expensive.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								It's better to wait until the user stops typing and then send a request.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								That's easy to implement with RxJS operators, as shown in this excerpt.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/package-search/package-search.component.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="debounce" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/package-search/package-search.component.ts (excerpt))">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2018-01-02 09:49:49 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `searchText$`  is the sequence of search-box values coming from the user.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-21 16:26:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								It's defined as an RxJS `Subject` , which means it is a multicasting `Observable` 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								that can also produce values for itself by calling `next(value)` ,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								as happens in the `search()`  method.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Rather than forward every `searchText`  value directly to the injected `PackageSearchService` ,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the code in `ngOnInit()`  _pipes_  search values through three operators:
							 
						 
					
						
							
								
									
										
										
										
											2017-03-31 12:23:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								1.  `debounceTime(500)`  - wait for the user to stop typing (1/2 second in this case). 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1.  `distinctUntilChanged()`  - wait until the search text changes. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1.  `switchMap()`  - send the search request to the service. 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The code sets `packages$`  to this re-composed `Observable`  of search results.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The template subscribes to `packages$`  with the [AsyncPipe ](api/common/AsyncPipe )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								and displays search results as they arrive.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A search value reaches the service only if it's a new value and the user has stopped typing.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-07-19 15:00:08 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< div  class = "alert is-helpful" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `withRefresh`  option is explained [below ](#cache-refresh ).
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### _switchMap()_
  
						 
					
						
							
								
									
										
										
										
											2018-01-02 09:49:49 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `switchMap()`  operator has three important characteristics.
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								1.  It takes a function argument that returns an `Observable` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`PackageSearchService.search`  returns an `Observable` , as other data service methods do. 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								2.  If a previous search request is still _in-flight_  (as when the connection is poor), 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								it cancels that request and sends a new one.
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								3.  It returns service responses in their original request order, even if the 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								server returns them out of order. 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-11 13:44:25 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-07-19 15:00:08 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< div  class = "alert is-helpful" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If you think you'll reuse this debouncing logic,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								consider moving it to a utility function or into the `PackageSearchService`  itself.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Intercepting requests and responses
  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								_HTTP Interception_ is a major feature of `@angular/common/http` . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								With interception, you declare _interceptors_  that inspect and transform HTTP requests from your application to the server.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The same interceptors may also inspect and transform the server's responses on their way back to the application.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Multiple interceptors form a _forward-and-backward_  chain of request/response handlers.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Interceptors can perform a variety of  _implicit_  tasks, from authentication to logging, in a routine, standard way, for every HTTP request/response. 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Without interception, developers would have to implement these tasks _explicitly_  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								for each `HttpClient`  method call.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### Write an interceptor
  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								To implement an interceptor, declare a class that implements the `intercept()`  method of the `HttpInterceptor`  interface.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 Here is a do-nothing _noop_  interceptor that simply passes the request through without touching it:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/http-interceptors/noop-interceptor.ts"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/http-interceptors/noop-interceptor.ts"
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `intercept`  method transforms a request into an `Observable`  that eventually returns the HTTP response. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In this sense, each interceptor is fully capable of handling the request entirely by itself.
							 
						 
					
						
							
								
									
										
										
										
											2017-05-10 10:06:04 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Most interceptors inspect the request on the way in and forward the (perhaps altered) request to the `handle()`  method of the `next`  object which implements the [`HttpHandler` ](api/common/http/HttpHandler ) interface.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-20 18:35:27 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								export abstract class HttpHandler {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  abstract handle(req: HttpRequest< any > ): Observable< HttpEvent < any > >;
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Like `intercept()` , the `handle()`  method transforms an HTTP request into an `Observable`  of [`HttpEvents` ](#httpevents ) which ultimately include the server's response. The `intercept()`  method could inspect that observable and alter it before returning it to the caller.
							 
						 
					
						
							
								
									
										
										
										
											2017-05-10 10:06:04 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This _no-op_  interceptor simply calls `next.handle()`  with the original request and returns the observable without doing a thing.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### The _next_ object
  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `next`  object represents the next interceptor in the chain of interceptors. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The final `next`  in the chain is the `HttpClient`  backend handler that sends the request to the server and receives the server's response.
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-20 18:35:27 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Most interceptors call `next.handle()`  so that the request flows through to the next interceptor and, eventually, the backend handler.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								An interceptor _could_  skip calling `next.handle()` , short-circuit the chain, and [return its own `Observable` ](#caching ) with an artificial server response. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This is a common middleware pattern found in frameworks such as Express.js.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Provide the interceptor
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `NoopInterceptor`  is a service managed by Angular's [dependency injection (DI) ](guide/dependency-injection ) system. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Like other services, you must provide the interceptor class before the app can use it.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Because interceptors are (optional) dependencies of the `HttpClient`  service, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								you must provide them in the same injector (or a parent of the injector) that provides `HttpClient` . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Interceptors provided _after_  DI creates the `HttpClient`  are ignored.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This app provides `HttpClient`  in the app's root injector, as a side-effect of importing the `HttpClientModule`  in `AppModule` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You should provide interceptors in `AppModule`  as well.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								After importing the `HTTP_INTERCEPTORS`  injection token from `@angular/common/http` ,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								write the `NoopInterceptor`  provider like this:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/http-interceptors/index.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="noop-provider" linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Note the `multi: true`  option. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This required setting tells Angular that `HTTP_INTERCEPTORS`  is a token for a _multiprovider_  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								that injects an array of values, rather than a single value.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You _could_  add this provider directly to the providers array of the `AppModule` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								However, it's rather verbose and there's a good chance that 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								you'll create more interceptors and provide them in the same way.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You must also pay [close attention to the order ](#interceptor-order ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								in which you provide these interceptors.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Consider creating a "barrel" file that gathers all the interceptor providers into an `httpInterceptorProviders`  array, starting with this first one, the `NoopInterceptor` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/http-interceptors/index.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="interceptor-providers"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/http-interceptors/index.ts" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Then import and add it to the `AppModule`  _providers array_  like this:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/app.module.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="interceptor-providers"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/app.module.ts (interceptor providers)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								As you create new interceptors, add them to the `httpInterceptorProviders`  array and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								you won't have to revisit the `AppModule` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-07-19 15:00:08 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< div  class = "alert is-helpful" >  
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								There are many more interceptors in the complete sample code.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Interceptor order
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Angular applies interceptors in the order that you provide them.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you provide interceptors _A_ , then _B_ , then _C_ ,  requests will flow in _A->B->C_  and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								responses will flow out _C->B->A_ .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You cannot change the order or remove interceptors later.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you need to enable and disable an interceptor dynamically, you'll have to build that capability into the interceptor itself.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### _HttpEvents_
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You may have expected the `intercept()`  and `handle()`  methods to return observables of `HttpResponse<any>`  as most `HttpClient`  methods do.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Instead they return observables of `HttpEvent<any>` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								That's because interceptors work at a lower level than those `HttpClient`  methods. A single HTTP request can generate multiple _events_ , including upload and download progress events. The `HttpResponse`  class itself is actually an event, whose type is `HttpEventType.HttpResponseEvent` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Many interceptors are only concerned with the outgoing request and simply return the event stream from `next.handle()`  without modifying it.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								But interceptors that examine and modify the response from `next.handle()`  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								will see all of these events. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Your interceptor should return _every event untouched_  unless it has a _compelling reason to do otherwise_ .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Immutability
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Although interceptors are capable of mutating requests and responses,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the `HttpRequest`  and `HttpResponse`  instance properties are `readonly` ,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								rendering them largely immutable.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								They are immutable for a good reason: the app may retry a request several times before it succeeds, which means that the interceptor chain may re-process the same request multiple times.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If 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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								TypeScript will prevent you from setting `HttpRequest`  readonly properties. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Typescript disallows the following assignment because req.url is readonly
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  req.url = req.url.replace('http://', 'https://');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								To alter the request, clone it first and modify the clone before passing it to `next.handle()` . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can clone and modify the request in a single step as in this example.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/http-interceptors/ensure-https-interceptor.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="excerpt" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/http-interceptors/ensure-https-interceptor.ts (excerpt)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `clone()`  method's hash argument allows you to mutate specific properties of the request while copying the others.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								##### The request body
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `readonly`  assignment guard can't prevent deep updates and, in particular, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								it can't prevent you from modifying a property of a request body object.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  req.body.name = req.body.name.trim(); // bad idea!
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If you must mutate the request body, copy it first, change the copy, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`clone()`  the request, and set the clone's body with the new body, as in the following example. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/http-interceptors/trim-name-interceptor.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="excerpt" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/http-interceptors/trim-name-interceptor.ts (excerpt)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								##### Clearing the request body
  
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Sometimes you need to clear the request body rather than replace it.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you set the cloned request body to `undefined` , Angular assumes you intend to leave the body as is.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								That is not what you want.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you set the cloned request body to `null` , Angular knows you intend to clear the request body.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  newReq = req.clone({ ... }); // body not mentioned => preserve original body
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  newReq = req.clone({ body: undefined }); // preserve original body
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  newReq = req.clone({ body: null }); // clear the body
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### Set default headers
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Apps often use an interceptor to set default headers on outgoing requests. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The sample app has an `AuthService`  that produces an authorization token.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Here is its `AuthInterceptor`  that injects that service to get the token and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								adds an authorization header with that token to every outgoing request:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/http-interceptors/auth-interceptor.ts"
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/http-interceptors/auth-interceptor.ts">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The practice of cloning a request to set new headers is so common that 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								there's a `setHeaders`  shortcut for it:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/http-interceptors/auth-interceptor.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="set-header-shortcut">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								An interceptor that alters headers can be used for a number of different operations, including:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  Authentication/authorization 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								*  Caching behavior; for example, `If-Modified-Since`  
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								*  XSRF protection 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Logging
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Because interceptors can process the request and response _together_ , they can do things like time and log 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								an entire HTTP operation. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Consider the following `LoggingInterceptor` , which captures the time of the request,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the time of the response, and logs the outcome with the elapsed time
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								with the injected `MessageService` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/http-interceptors/logging-interceptor.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="excerpt" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/http-interceptors/logging-interceptor.ts)">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The RxJS `tap`  operator captures whether the request succeed or failed.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The RxJS `finalize`  operator is called when the response observable either errors or completes (which it must),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								and reports the outcome to the `MessageService` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Neither `tap`  nor `finalize`  touch the values of the observable stream returned to the caller.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Caching
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Interceptors can handle requests by themselves, without forwarding to `next.handle()` .
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For example, you might decide to cache certain requests and responses to improve performance.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can delegate caching to an interceptor without disturbing your existing data services. 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-20 18:35:27 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `CachingInterceptor`  demonstrates this approach.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/http-interceptors/caching-interceptor.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="v1" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/http-interceptors/caching-interceptor.ts)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `isCachable()`  function determines if the request is cachable.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In this sample, only GET requests to the npm package search api are cachable.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If the request is not cachable, the interceptor simply forwards the request 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								to the next handler in the chain.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If a cachable request is found in the cache, the interceptor returns an `of()`  _observable_  with
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the cached response, by-passing the `next`  handler (and all other interceptors downstream).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If a cachable request is not in cache, the code calls `sendRequest` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{@a  send-request}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/http-interceptors/caching-interceptor.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="send-request">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `sendRequest`  function creates a [request clone ](#immutability ) without headers
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								because the npm api forbids them.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								It forwards that request to `next.handle()`  which ultimately calls the server and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								returns the server's response.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Note how `sendRequest`  _intercepts the response_  on its way back to the application.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								It _pipes_  the response through the `tap()`  operator,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								whose callback adds the response to the cache.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The original response continues untouched back up through the chain of interceptors
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								to the application caller. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Data services, such as `PackageSearchService` , are unaware that 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								some of their `HttpClient`  requests actually return cached responses.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{@a  cache-refresh}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Return a multi-valued _Observable_
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `HttpClient.get()`  method normally returns an _observable_  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								that either emits the data or an error. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Some folks describe it as a "_one and done_" observable.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								But an interceptor can change this to an _observable_  that emits more than once.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								A revised version of the `CachingInterceptor`  optionally returns an _observable_  that
							 
						 
					
						
							
								
									
										
										
										
											2018-03-21 16:26:50 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								immediately emits the cached response, sends the request to the NPM web API anyway,
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								and emits again later with the updated search results.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/http-interceptors/caching-interceptor.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="intercept-refresh">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The _cache-then-refresh_  option is triggered by the presence of a **custom `x-refresh` header** .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-07-19 15:00:08 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< div  class = "alert is-helpful" >  
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								A checkbox on the `PackageSearchComponent`  toggles a `withRefresh`  flag,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								which is one of the arguments to `PackageSearchService.search()` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								That `search()`  method creates the custom `x-refresh`  header
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								and adds it to the request before calling `HttpClient.get()` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The revised `CachingInterceptor`  sets up a server request 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								whether there's a cached value or not, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								using the same `sendRequest()`  method described [above ](#send-request ).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `results$`  observable will make the request when subscribed.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If there's no cached value, the interceptor returns `results$` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If there is a cached value, the code _pipes_  the cached response onto
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`results$` , producing a recomposed observable that emits twice, 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the cached response first (and immediately), followed later
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								by the response from the server.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Subscribers see a sequence of _two_  responses.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Listening to progress events
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Sometimes applications transfer large amounts of data and those transfers can take a long time.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								File uploads are a typical example. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Give the users a better experience by providing feedback on the progress of such transfers.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								To make a request with progress events enabled, you can create an instance of `HttpRequest`  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								with the `reportProgress`  option set true to enable tracking of progress events.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/uploader/uploader.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="upload-request" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/uploader/uploader.service.ts (upload request)">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< div  class = "alert is-important" >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Every progress event triggers change detection, so only turn them on if you truly intend to report progress in the UI.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Next, pass this request object to the `HttpClient.request()`  method, which
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								returns an `Observable`  of `HttpEvents` , the same events processed by interceptors:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/uploader/uploader.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="upload-body" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/uploader/uploader.service.ts (upload body)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `getEventMessage`  method interprets each type of `HttpEvent`  in the event stream.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/uploader/uploader.service.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="getEventMessage" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/uploader/uploader.service.ts (getEventMessage)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  class = "alert is-helpful" >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The sample app for this guide doesn't have a server that accepts uploaded files.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `UploadInterceptor`  in `app/http-interceptors/upload-interceptor.ts`  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								intercepts and short-circuits upload requests
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								by returning an observable of simulated events.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 16:59:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Security: XSRF Protection
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[Cross-Site Request Forgery (XSRF) ](https://en.wikipedia.org/wiki/Cross-site_request_forgery ) is an attack technique by which the attacker can trick an authenticated user into unknowingly executing actions on your website. `HttpClient`  supports a [common mechanism ](https://en.wikipedia.org/wiki/Cross-site_request_forgery#Cookie-to-Header_Token ) used to prevent XSRF attacks. When performing HTTP requests, an interceptor reads a token from a cookie, by default `XSRF-TOKEN` , and sets it as an HTTP header, `X-XSRF-TOKEN` . Since 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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-20 18:35:27 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								By default, an interceptor sends this cookie on all mutating requests (POST, etc.)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								to relative URLs but not on GET/HEAD requests or
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 16:59:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								on requests with an absolute URL.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-20 18:35:27 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								To take advantage of this, your server needs to set a token in a JavaScript readable session cookie called `XSRF-TOKEN`  on either the page load or the first GET request. On subsequent requests the server can verify that the cookie matches the `X-XSRF-TOKEN`  HTTP header, and therefore be sure that only code running on your domain could have sent the request. The token must be unique for each user and must be verifiable by the server; this prevents the client from making up its own tokens. Set the token to a digest of your site's authentication
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 16:59:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								cookie with a salt for added security.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In order to prevent collisions in environments where multiple Angular apps share the same domain or subdomain, give each application a unique cookie name.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  class = "alert is-important" >  
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*Note that `HttpClient`  supports only the client half of the XSRF protection scheme.* 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Your backend service must be configured to set the cookie for your page, and to verify that 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the header is present on all eligible requests. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If not, Angular's default protection will be ineffective.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 16:59:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Configuring custom cookie/header names
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If your backend service uses different names for the XSRF token cookie or header, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								use `HttpClientXsrfModule.withOptions()`  to override the defaults.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 16:59:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/app/app.module.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="xsrf" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-07-13 16:59:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Testing HTTP requests
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Like any external dependency, the HTTP backend needs to be mocked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								so your tests can simulate interaction with a remote server. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `@angular/common/http/testing`  library makes 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								setting up such mocking straightforward.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Mocking philosophy
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Angular's HTTP testing library is designed for a pattern of testing wherein 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-03 00:24:36 +07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								the app executes code and makes requests first.
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Then a test expects that certain requests have or have not been made, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								performs assertions against those requests, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								and finally provide responses by "flushing" each expected request.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								At the end, tests may verify that the app has made no unexpected requests.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  class = "alert is-helpful" >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can run < live-example  stackblitz = "specs" > these sample tests< / live-example >  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								in a live coding environment.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The tests described in this guide are in `src/testing/http-client.spec.ts` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								There are also tests of an application data service that call `HttpClient`  in
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`src/app/heroes/heroes.service.spec.ts` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Setup
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								To begin testing calls to `HttpClient` , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import the `HttpClientTestingModule`  and the mocking controller, `HttpTestingController` ,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								along with the other symbols your tests require.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/testing/http-client.spec.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="imports" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/testing/http-client.spec.ts (imports)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Then add the `HttpClientTestingModule`  to the `TestBed`  and continue with
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the setup of the _service-under-test_ .
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/testing/http-client.spec.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="setup" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/testing/http-client.spec.ts(setup)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Now requests made in the course of your tests will hit the testing backend instead of the normal backend.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This setup also calls `TestBed.get()`  to inject the `HttpClient`  service and the mocking controller
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								so they can be referenced during the tests.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Expecting and answering requests
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Now you can write a test that expects a GET Request to occur and provides a mock response. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/testing/http-client.spec.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="get-test" 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-11 13:29:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header="app/testing/http-client.spec.ts(httpClient.get)" linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The last step, verifying that no requests remain outstanding, is common enough for you to move it into an `afterEach()`  step:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/testing/http-client.spec.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="afterEach" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### Custom request expectations
  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If matching by URL isn't sufficient, it's possible to implement your own matching function. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, you could look for an outgoing request that has an authorization header:
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/testing/http-client.spec.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="predicate" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								As with the previous `expectOne()` , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the test will fail if 0 or 2+ requests satisfy this predicate.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Handling more than one request
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If you need to respond to duplicate requests in your test, use the `match()`  API instead of `expectOne()` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								It takes the same arguments but returns an array of matching requests. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Once returned, these requests are removed from future matching and 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								you are responsible for flushing and verifying them.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-13 15:22:07 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/testing/http-client.spec.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="multi-request" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Testing for errors
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You should test the app's defenses against HTTP requests that fail.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-27 20:44:07 +10:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Call `request.flush()`  with an error message, as seen in the following example.
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/testing/http-client.spec.ts"
							 
						 
					
						
							
								
									
										
										
										
											2018-06-28 21:48:45 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  region="404"
							 
						 
					
						
							
								
									
										
										
										
											2018-01-05 01:46:55 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  linenums="false">
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2018-04-27 20:44:07 +10:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Alternatively, you can call `request.error()`  with an `ErrorEvent` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< code-example  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  path="http/src/testing/http-client.spec.ts"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  region="network-error"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  linenums="false">
							 
						 
					
						
							
								
									
										
										
										
											2018-05-03 11:54:46 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / code-example >