import { Http } from '@angular/http'; import { Observable } from 'rxjs'; export class MyComponent { constructor(private http: Http) {} fetchData(): Observable{ const url = 'https://jsonplaceholder.typicode.com/users'; return this.http.get(url).map(res => res.json()); } }
import { Http, Headers } from '@angular/http'; export class MyComponent { constructor(private http: Http) {} saveData() { const url = 'https://jsonplaceholder.typicode.com/users'; const body = { name: 'John Doe', email: 'johndoe@example.com' }; const headers = new Headers({ 'Content-Type': 'application/json' }); return this.http.post(url, JSON.stringify(body), { headers }) .map(res => res.json()); } }This example shows how to make a POST request to the JSONPlaceholder API to create a new user. The Http service is injected into the constructor of the component and then used to make the request. The body of the request is a JSON object, which is converted to a string using the `JSON.stringify()` method. The HttpHeaders class is used to set the Content-Type header to application/json. The response is then converted to a JSON object using the map operator from the RxJS library.