export HTTP_PROVIDERS : any[]
export HTTP_PROVIDERS : any[]
exported from angular2/http defined in angular2/http.ts (line 154)
Provides a basic set of injectables to use the Http
service in any application.
The HTTP_PROVIDERS
should be included either in a component's injector,
or in the root injector when bootstrapping an application.
Example (live demo)
The primary public API included in HTTP_PROVIDERS
is the Http
class.
However, other providers required by Http
are included,
which may be beneficial to override in certain cases.
The providers included in HTTP_PROVIDERS
include:
Http
XHRBackend
BrowserXHR
- Private factory to createXMLHttpRequest
instancesRequestOptions
- Bound toBaseRequestOptions
classResponseOptions
- Bound toBaseResponseOptions
class
There may be cases where it makes sense to extend the base request options,
such as to add a search string to be appended to all URLs.
To accomplish this, a new provider for RequestOptions
should
be added in the same injector as HTTP_PROVIDERS
.
Example (live demo)
Likewise, to use a mock backend for unit tests, the XHRBackend
provider should be bound to MockBackend
.
Example (live demo)
import {provide} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {HTTP_PROVIDERS, Http, Response, XHRBackend} from 'angular2/http';
import {MockBackend} from 'angular2/http/testing';
var people = [{name: 'Jeff'}, {name: 'Tobias'}];
var injector = Injector.resolveAndCreate([
HTTP_PROVIDERS,
MockBackend,
provide(XHRBackend, {useExisting: MockBackend})
]);
var http = injector.get(Http);
var backend = injector.get(MockBackend);
// Listen for any new requests
backend.connections.observer({
next: connection => {
var response = new Response({body: people});
setTimeout(() => {
// Send a response to the request
connection.mockRespond(response);
});
});
http.get('people.json').observer({
next: res => {
// Response came from mock backend
console.log('first person', res.json()[0].name);
}
});