Besides all the answers below, if I hide behind some additional points Here is Http how to use / import everything ...
ANGULAR2 HTTP (UPDATED to Beta !!)
First, as the name implies, we must import the http file into index.html, like this
<script src="node_modules/angular2/bundles/http.dev.js"></script>
or you can update it via CDN from here
then in the next step we need to import Http and HTTP_PROVIDERS from the packages provided by angular.
but yes, itβs good practice to provide HTTP_PROVIDERS in the bootstrap file, because that way it is provided globally and is available to the entire project, as shown below.
bootstrap(App, [ HTTP_PROVIDERS, some_more_dependency's ]);
and import from ....
import {http} from 'angular2/http';
Use Restover or json API with Http
Now, along with http, we have several more options provided with angular2 / http ie, for example, Headers, Request, Requestoptions, etc. etc. which is mainly used when using the Rest API or Json temporary data. Firstly, we must import it all as follows:
import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from 'angular2/http';
sometimes we need to provide headers, consuming the API to send access_token and much more, which is done as follows:
this.headers = new Headers(); this.headers.append("Content-Type", 'application/json'); this.headers.append("Authorization", 'Bearer ' + localStorage.getItem('id_token'));
now come to RequestMethods: basically we use GET, POST, but we have another option that you can here ...
we can use usage request methods using RequestMethod.method_name
There is another option for the API, now I posted one example of a POST request using several important methods:
PostRequest(url,data) { this.headers = new Headers(); this.headers.append("Content-Type", 'application/json'); this.headers.append("Authorization", 'Bearer ' + localStorage.getItem('id_token')) this.requestoptions = new RequestOptions({ method: RequestMethod.Post, url: url, headers: this.headers, body: JSON.stringify(data) }) return this.http.request(new Request(this.requestoptions)) .map((res: Response) => { if (res) { return [{ status: res.status, json: res.json() }] } }); }
for more information, I found the two best links here .. and here ...