W3cubDocs

/Angular

Http

class deprecated

Performs http requests using XMLHttpRequest as the default backend.

Deprecation Notes

see https://angular.io/guide/http

class Http {
  constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions)
  protected _backend: ConnectionBackend
  protected _defaultOptions: RequestOptions
  request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>
  get(url: string, options?: RequestOptionsArgs): Observable<Response>
  post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>
  put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>
  delete(url: string, options?: RequestOptionsArgs): Observable<Response>
  patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>
  head(url: string, options?: RequestOptionsArgs): Observable<Response>
  options(url: string, options?: RequestOptionsArgs): Observable<Response>
}

Subclasses

Description

Http is available as an injectable class, with methods to perform http requests. Calling request returns an Observable which will emit a single Response when a response is received.

Example

import {Http, HTTP_PROVIDERS} from '@angular/http';
import {map} from 'rxjs/operators';

@Component({
  selector: 'http-app',
  viewProviders: [HTTP_PROVIDERS],
  templateUrl: 'people.html'
})
class PeopleComponent {
  constructor(http: Http) {
    http.get('people.json')
      // Call map on the response observable to get the parsed people object
      .pipe(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);
  }
}

Example

http.get('people.json').subscribe((res:Response) => this.people = res.json());

The default construct used to perform requests, XMLHttpRequest, is abstracted as a "Backend" ( XHRBackend in this case), which could be mocked with dependency injection by replacing the XHRBackend provider, as in the following example:

Example

import {BaseRequestOptions, Http} from '@angular/http';
import {MockBackend} from '@angular/http/testing';
var injector = Injector.resolveAndCreate([
  BaseRequestOptions,
  MockBackend,
  {provide: Http, useFactory:
      function(backend, defaultOptions) {
        return new Http(backend, defaultOptions);
      },
      deps: [MockBackend, BaseRequestOptions]}
]);
var http = injector.get(Http);
http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res));

Constructor

constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions)

Parameters

_backend

Type: ConnectionBackend.

_defaultOptions

Type: RequestOptions.

Properties

Property Type Description
_backend ConnectionBackend Declared in constructor.
_defaultOptions RequestOptions Declared in constructor.

Methods

request()

Performs any type of http request. First argument is required, and can either be a url or a Request instance. If the first argument is a url, an optional RequestOptions object can be provided as the 2nd argument. The options object will be merged with the values of BaseRequestOptions before performing the request.

request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>

Parameters

url

Type: string | Request.

options

Type: RequestOptionsArgs.

Optional. Default is undefined.

Returns

Observable<Response>

get()

Performs a request with get http method.

get(url: string, options?: RequestOptionsArgs): Observable<Response>

Parameters

url

Type: string.

options

Type: RequestOptionsArgs.

Optional. Default is undefined.

Returns

Observable<Response>

post()

Performs a request with post http method.

post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>

Parameters

url

Type: string.

body

Type: any.

options

Type: RequestOptionsArgs.

Optional. Default is undefined.

Returns

Observable<Response>

put()

Performs a request with put http method.

put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>

Parameters

url

Type: string.

body

Type: any.

options

Type: RequestOptionsArgs.

Optional. Default is undefined.

Returns

Observable<Response>

delete()

Performs a request with delete http method.

delete(url: string, options?: RequestOptionsArgs): Observable<Response>

Parameters

url

Type: string.

options

Type: RequestOptionsArgs.

Optional. Default is undefined.

Returns

Observable<Response>

patch()

Performs a request with patch http method.

patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>

Parameters

url

Type: string.

body

Type: any.

options

Type: RequestOptionsArgs.

Optional. Default is undefined.

Returns

Observable<Response>

head()

Performs a request with head http method.

head(url: string, options?: RequestOptionsArgs): Observable<Response>

Parameters

url

Type: string.

options

Type: RequestOptionsArgs.

Optional. Default is undefined.

Returns

Observable<Response>

options()

Performs a request with options http method.

options(url: string, options?: RequestOptionsArgs): Observable<Response>

Parameters

url

Type: string.

options

Type: RequestOptionsArgs.

Optional. Default is undefined.

Returns

Observable<Response>

Annotations

@Injectable()

© 2010–2018 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/api/http/Http