Sunday, December 15, 2019

Angular - HTTP POST Request Examples

Angular - HTTP POST Request Examples

Simple POST request with a JSON body and response type <any>
This sends an HTTP POST request to the JSON Placeholder api which is a fake online REST api that includes a /posts route that responds to POST requests with the contents of the post body and an id property.
HTTP POST Request Examples

The id from the response is assigned to the local postId property in the subscribe callback function.
The response type is set to <any> so it handle any properties returned in the response.
this.http.post<any>('https://jsonplaceholder.typicode.com/posts', { title: 'Angular POST Request Example' }).subscribe(data => {
    this.postId = data.id;
})
POST request with strongly typed response
This sends the same request as the above but sets the response type to a custom Article interface that defines the expected response properties.
interface Article {
    id: number;
    title: string;
}
this.http.post<Article>('https://jsonplaceholder.typicode.com/posts', { title: 'Angular POST Request Example' }).subscribe(data => {
    this.postId = data.id;
})

POST request with error handling
This sends a request to an invalid url on the api and logs the error to the console from an error handler function that is passed as the second parameter to the subscribe() method of the Observable returned from the call to http.post().
this.http.post('https://jsonplaceholder.typicode.com/invalid-url', { title: 'Angular POST Request Example' }).subscribe({
    next: data => this.postId = data.id,
    error: error => console.error('There was an error!', error)
})
Prerequisites for making HTTP requests from Angular
Before making HTTP requests from your Angular app you need to do a couple of things.
1. Add the HttpClientModule to the imports array of your AppModule like below on lines 3 and 10.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
@NgModule({
    imports: [
        BrowserModule,
        HttpClientModule
    ],
    declarations: [
        AppComponent
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

2. Import the HttpClient into your component and add it to the constructor() params like below on lines 2 and 8.
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({ selector: 'app', templateUrl: 'app.component.html' })
export class AppComponent implements OnInit {
    postId;
    constructor(private http: HttpClient) { }
    ngOnInit() {     
        // Simple POST request with a JSON body and response type <any>
        this.http.post<any>('https://jsonplaceholder.typicode.com/posts', { title: 'Angular POST Request Example' }).subscribe(data => {
            this.postId = data.id;
        })
    }
}
source : https://jasonwatmore.com/post/2019/11/21/angular-http-post-request-examples

No comments:

Post a Comment