200 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
		
		
			
		
	
	
			200 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								p.location-badge.
							 | 
						||
| 
								 | 
							
								  exported from <a href='../http'>angular2/http</a>
							 | 
						||
| 
								 | 
							
								  defined in <a href="https://github.com/angular/angular/tree/3a0410a/modules/angular2/src/http/http.ts#L22-L142">angular2/src/http/http.ts (line 22)</a>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								:markdown
							 | 
						||
| 
								 | 
							
								  Performs http requests using `XMLHttpRequest` as the default backend.
							 | 
						||
| 
								 | 
							
								  
							 | 
						||
| 
								 | 
							
								  `Http` is available as an injectable class, with methods to perform http requests. Calling
							 | 
						||
| 
								 | 
							
								  `request` returns an
							 | 
						||
| 
								 | 
							
								  [Observable](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/observable.md),
							 | 
						||
| 
								 | 
							
								  which will emit a single <a href='Response-class.html'><code>Response</code></a> when a response is
							 | 
						||
| 
								 | 
							
								  received.
							 | 
						||
| 
								 | 
							
								  
							 | 
						||
| 
								 | 
							
								  #Example
							 | 
						||
| 
								 | 
							
								  
							 | 
						||
| 
								 | 
							
								  ```
							 | 
						||
| 
								 | 
							
								  import {Http, httpInjectables} from 'angular2/http';
							 | 
						||
| 
								 | 
							
								  @Component({selector: 'http-app', appInjector: [httpInjectables]})
							 | 
						||
| 
								 | 
							
								  @View({templateUrl: 'people.html'})
							 | 
						||
| 
								 | 
							
								  class PeopleComponent {
							 | 
						||
| 
								 | 
							
								    constructor(http: Http) {
							 | 
						||
| 
								 | 
							
								      http('people.json')
							 | 
						||
| 
								 | 
							
								        // Call map on the response observable to get the parsed people object
							 | 
						||
| 
								 | 
							
								        .map(res => res.json())
							 | 
						||
| 
								 | 
							
								        // Subscribe to the observable to get the parsed people object and attach it to the
							 | 
						||
| 
								 | 
							
								        // component
							 | 
						||
| 
								 | 
							
								        .subscribe(people => this.people = people);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  ```
							 | 
						||
| 
								 | 
							
								  
							 | 
						||
| 
								 | 
							
								  The default construct used to perform requests, `XMLHttpRequest`, is abstracted as a "Backend" (
							 | 
						||
| 
								 | 
							
								  <a href='XHRBackend-class.html'><code>XHRBackend</code></a> in this case), which could be mocked with dependency injection by replacing
							 | 
						||
| 
								 | 
							
								  the <a href='XHRBackend-class.html'><code>XHRBackend</code></a> binding, as in the following example:
							 | 
						||
| 
								 | 
							
								  
							 | 
						||
| 
								 | 
							
								  #Example
							 | 
						||
| 
								 | 
							
								  
							 | 
						||
| 
								 | 
							
								  ```
							 | 
						||
| 
								 | 
							
								  import {MockBackend, BaseRequestOptions, Http} from 'angular2/http';
							 | 
						||
| 
								 | 
							
								  var injector = Injector.resolveAndCreate([
							 | 
						||
| 
								 | 
							
								    BaseRequestOptions,
							 | 
						||
| 
								 | 
							
								    MockBackend,
							 | 
						||
| 
								 | 
							
								    bind(Http).toFactory(
							 | 
						||
| 
								 | 
							
								        function(backend, defaultOptions) {
							 | 
						||
| 
								 | 
							
								          return new Http(backend, defaultOptions);
							 | 
						||
| 
								 | 
							
								        },
							 | 
						||
| 
								 | 
							
								        [MockBackend, BaseRequestOptions])
							 | 
						||
| 
								 | 
							
								  ]);
							 | 
						||
| 
								 | 
							
								  var http = injector.get(Http);
							 | 
						||
| 
								 | 
							
								  http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res));
							 | 
						||
| 
								 | 
							
								  ```
							 | 
						||
| 
								 | 
							
								  
							 | 
						||
| 
								 | 
							
								  
							 | 
						||
| 
								 | 
							
								.l-main-section
							 | 
						||
| 
								 | 
							
								  h2 Members
							 | 
						||
| 
								 | 
							
								  .l-sub-section
							 | 
						||
| 
								 | 
							
								    h3 constructor
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    pre.prettyprint
							 | 
						||
| 
								 | 
							
								      code.
							 | 
						||
| 
								 | 
							
								        constructor(_backend: XHRBackend, _defaultOptions: BaseRequestOptions)
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    :markdown
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  .l-sub-section
							 | 
						||
| 
								 | 
							
								    h3 request
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    pre.prettyprint
							 | 
						||
| 
								 | 
							
								      code.
							 | 
						||
| 
								 | 
							
								        request(url: string | Request, options?: IRequestOptions)
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    :markdown
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      Performs any type of http request. First argument is required, and can either be a url or
							 | 
						||
| 
								 | 
							
								      a <a href='Request-class.html'><code>Request</code></a> instance. If the first argument is a url, an optional <a href='RequestOptions-class.html'><code>RequestOptions</code></a>
							 | 
						||
| 
								 | 
							
								      object can be provided as the 2nd argument. The options object will be merged with the values
							 | 
						||
| 
								 | 
							
								      of <a href='BaseRequestOptions-class.html'><code>BaseRequestOptions</code></a> before performing the request.
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  .l-sub-section
							 | 
						||
| 
								 | 
							
								    h3 get
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    pre.prettyprint
							 | 
						||
| 
								 | 
							
								      code.
							 | 
						||
| 
								 | 
							
								        get(url: string, options?: IRequestOptions)
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    :markdown
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      Performs a request with `get` http method.
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  .l-sub-section
							 | 
						||
| 
								 | 
							
								    h3 post
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    pre.prettyprint
							 | 
						||
| 
								 | 
							
								      code.
							 | 
						||
| 
								 | 
							
								        post(url: string, body: URLSearchParams | FormData | Blob | string, options?: IRequestOptions)
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    :markdown
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      Performs a request with `post` http method.
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  .l-sub-section
							 | 
						||
| 
								 | 
							
								    h3 put
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    pre.prettyprint
							 | 
						||
| 
								 | 
							
								      code.
							 | 
						||
| 
								 | 
							
								        put(url: string, body: URLSearchParams | FormData | Blob | string, options?: IRequestOptions)
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    :markdown
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      Performs a request with `put` http method.
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  .l-sub-section
							 | 
						||
| 
								 | 
							
								    h3 delete
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    pre.prettyprint
							 | 
						||
| 
								 | 
							
								      code.
							 | 
						||
| 
								 | 
							
								        delete(url: string, options?: IRequestOptions)
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    :markdown
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      Performs a request with `delete` http method.
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  .l-sub-section
							 | 
						||
| 
								 | 
							
								    h3 patch
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    pre.prettyprint
							 | 
						||
| 
								 | 
							
								      code.
							 | 
						||
| 
								 | 
							
								        patch(url: string, body: URLSearchParams | FormData | Blob | string, options?: IRequestOptions)
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    :markdown
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      Performs a request with `patch` http method.
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  .l-sub-section
							 | 
						||
| 
								 | 
							
								    h3 head
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    pre.prettyprint
							 | 
						||
| 
								 | 
							
								      code.
							 | 
						||
| 
								 | 
							
								        head(url: string, options?: IRequestOptions)
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    :markdown
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      Performs a request with `head` http method.
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								      
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 |