How to post data to the database using Angular 2 http post request

In this article, I am going to explain to you how to post data using angular 2 Http Post request ,  the example which I am going to take out here is submit a comment to the article, for this, I am going to build a separate comments component using JSON placeholder which is a fake rest API and we are going to use only for testing purpose, this API takes JSON Object as an input parameter something like below

Building comments component

posts.comment.html

The first thing which I am going to do out here is design and build an input form component  for comment’s, if you are new to angular 2 then refer my other article on how to build angular 2 component , the input form component allows end user to enter name, email, body and on click of submit button it sends data to the comments service

Here, I am using angular 2 two-way data binding to read user entered values from the input fields, we need to add two-way binding to name, email and comment box, the syntax for the two-way data binding called is a banana in the box “[()]”, check below Html for two-way binding syntax

Message.ts

I have created a separate class Message.ts using typescript, if you are familiar with Java then it is something like a Bean, instead of declaring every property with the @Input directive I have created a separate class and declared with @Input directive this class contains four property’s such as name, email, postId and comments

app.posts.submit.component.ts

Now build a angular2 component for comments and it should implement ngOnInit method, in this method, I have created a new Message object, the message object takes name and postId which are initialized with default values “keys and strokes” and 1 because we are not dynamically fetching postId and username here, when you integrate with some other component then pass these values dynamically but here as we are using fake JSON API so we have to initialize postId default value to 1 and name of the user to “keys and strokes”

The subscribe takes three functions data , error and completed , the data function executes when the Http call is successful here we are assigning returned data to the responseStatus object

Tthe error function executes when there is an error thrown from the Http call, the complete function executes when the Http call is completed here you can log a message that the request is completed

app.comments.service.ts

The postComments method accepts Message that is passed down from the component when the click of post comment button, the Http calls returns observables in Angular 2 so we must import RXjs to operate them, the Http Post request takes two parameters post_url and Message object and returns response as JSON object,

Go to the command prompt of the application folder and run the “npm start” to see comments component on the browser , the component renders like below, click of post submit button the status will be changed to congratulation message

comments

  • Johnrel

    Hi Raj!

    Thank you for this tutorial , this is one of the best tutorial for newbie like me. I am starting learning on angular 2 specially on CRUD, i found that this tutorial is very helpful although i have some question but it’s perfectly awesome. I hope i can connect with you , you can reach me at skype: realtrue13

    Thank you!

  • Thierno

    Hi raj, thank you for this great. if my getting the postid from the database, how can I proceed here.

    • Raj Mandalapu

      you need to pass post id to the message object, every post will have id, you need to get it pass it to the message object

      ngOnInit() {
      this.message = new Message();
      this.message.name = “keysandstokes”;
      this.message.postId=1;
      }

      • Thierno

        thank you Raj, I am able to fix it. I was doing the same thing but was failing to call the correct variable. great article, I bookmarked your site