Assuming you have imported all required files for http if not then read out this answer
First of all you have to decorate your http service class with decorator i.r @Injectable()
, and there are lot of way of using http but as i am using which method ,i am posting here :-
To make Post request sometimes we have to send autorization key also something else by appending with headers so we have to use headers like this :-
this.headers = new Headers();
this.headers.append("Content-Type", 'application/json');
this.headers.append("Authorization", 'Bearer ' + key)
Than you can use request method as you required i.e Get, Post, Put, Delete.
here is simple example of Post request using http :-
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() }]
}
});
}
working example for Get request :-
Working Example of Get request
see also :-