Angular Http Post Request Example
Here, i will give you example of how to send http post request in angular application. we can easily send angular post request with headers and body. i will give you simple example of call api ajax request with angular. you can send ajax request with angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17 too.
Every App must important to sending api request to another server. we can use http client request and get data and store data information to our server. as specially when you are working with angular, vue, react application. you must have to learn how to run http client request with angular 8.
Here, i will give you very simple example to get all data using api and store data using api. we will use jsonplaceholder api for testing now. so we don't require to create new api for it.
So, let's see bellow example step by step how to create http service and how to use it.
Step 1: Create New App
You can easily create your angular app using bellow command:
ng new my-new-app
Step 2: Import HttpClientModule
In this step, we need to import HttpClientModule to app.module.ts file. so let's import it as like bellow:
src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Step 3: Create Service for API
Here, we need to create service for http client request. we will create service file and write client http request code. this service will use in our component file. So let's create service and put bellow code:
ng g s services/post
Now let's add code as like bellow:
src/app/services/post.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class PostService {
private url = 'http://jsonplaceholder.typicode.com/posts';
constructor(private httpClient: HttpClient) { }
getPosts(){
return this.httpClient.get(this.url);
}
create(post){
return this.httpClient.post(this.url, JSON.stringify(post));
}
}
Step 4: Use Service to Component
Now we have to use this services to our app component. So let's updated code as like bellow:
src/app/app.component.ts
import { Component, OnInit } from '@angular/core';
import { PostService } from './services/post.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
posts;
constructor(private service:PostService) {}
ngOnInit() {
this.service.getPosts()
.subscribe(response => {
this.posts = response;
});
}
createPost(input: HTMLInputElement){
let post = {title: input.value};
input.value = '';
this.service.create(post)
.subscribe((response: { id }) => {
post['id'] = response.id;
this.posts.splice(0,0, post);
});
}
}
Step 5: Updated View File
Now here, we will updated our html file. let's put bellow code:
src/app/app.component.html
<h1>Angular 8 HttpClient for Sending Http Request Example - ItSolutionStuff.com</h1>
<input
(keyup.enter)="createPost(title)" #title
type="text" class="form-control">
<ul class="list-group">
<li
*ngFor="let post of posts"
class="list-group-item">
{{ post.title }}
</li>
</ul>
Now we are ready to run our example, you can run by following command:
ng serve
you will see layout as bellow:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Upload Multiple Images in Angular?
- How to use Highcharts in Angular?
- Angular Material Datepicker Example
- How to Install And Use JQuery in Angular?
- Angular Toggle a Div on Button Click Example
- Angular Service with Httpclient Example
- Angular Get Parameters from URL Route Example
- Angular 9/8 HttpClient for Sending Http Request Example
- Angular Get Current Route Name Example
- How to Get Current URL in Angular?
- Angular 9/8 Routing and Nested Routing Tutorial With Example
- Angular Change Date Format in Component Example
- Angular Font Awesome - How to install font awesome in Angular 9/8?
- How to Create Custom Validators in Angular 9/8?