HttpPost Method In ASP.NET Web API - Part Four

In the previous articles of the ASP.NET Web API series, I’ve used the Get() method of HTTP to make a lot of requests by which we can get a member or read the data. Hence, to retrieve the information as per our expectations, we use the HttpGet method by using a URL. In this article of the series, I’ll work on the Post() method of HTTP. You can follow the ASP.NET Web API series for the purpose of learning how the Get() method request works in the following links –

Now, we’ll work on another method of HTTP, that is Post() method, which is responsible to send the data to the Server. I’m going to work on my previous project entity framework that I have created in the previous article of the series,  in Part Three.

In this, I will show you how we can send the data, which will be saved in my SQL database by using Post() method. Hence, just flip to Visual Studio and open your previous project.

Under the IEmployeTest Interface, create a method to insert a record of an employee, followed by the table name, that is Employee.
  1. string insertEmploye(Employe emp);  

Here is the screenshot of IEmployeInterface -

 
  
In the EmployeTest class, create two instances; one for EmployeEntites, which is our context, and another for the table by which we can insert the detail of employee. EmpID is set to primary key, which is not required to be called here.
  1. public string insertEmploye(Employe emp)  
  2. {  
  3.     EmployeeEntities EM = new EmployeeEntities();  
  4.     Employe ems = new Employe();  
  5.     ems.EmpID = emp.EmpID;  
  6.     ems.EmpName = emp.EmpName;  
  7.     ems.EmpAddress = emp.EmpAddress;  
  8.     ems.EmpMoNo = emp.EmpMoNo;  
  9.     ems.EmpProfession = emp.EmpProfession;  
  10.     EM.Employes.Add(ems);  
  11.     EM.SaveChanges();  
  12.     return "Hey!! congrates.. Amit your Employee data is saved successfully";  
  13. }     
You can follow the screenshot of EmployeTest class, given below:
 


In the Employecontroller, I need to call insertetEmploye() method with the help of empTest.
  1. [HttpPost]  
  2. public string InsertEmploye(Employe em)  
  3. {  
  4.     return empTest.insertEmploye(em);  
  5. }       

Here is the screenshot of Employee controller: 



All is set now and we can call the POST method, using Postman.

API Testing with Postman

Postman is a tool that interacts with HTTP APIs and produces HTTP methods which you want to return. Postman is responsible to create, send, and save HTTP requests and afterwards test the response data which you want to expect. Postman is a Google Chrome app, which also has powerful testing features.

In the Header tab, I need to specify what kind of response a client can accept. It is a HTTP header. In Postman, we can change the data in different formats. If we want to get our data in JSON format, we need to write: 

Key = Content-Type value = application/json

Hence, the content-type is our key and Application/json is our value. Under the value, we can pass our response data in whichever format we want to return the data.
 


I want to call Post method. Hence, select POST method from the list of HTTP methods. 

We try and pass the Web API URL in the URL tab in order to make sure you need to first run your project and pass localhost URL.
 


Go to body panel and select row. Under the row, write the JSON script of your data which you want to save in the database. After writing the JSON script of employee data, click the Send button of the Window.
 


Hence, you can see my data has been saved successfully. You can refer to the history of your all operations in the left side of the Postman Window.
 


Let’s check in to SQL Server Management. I have my table of Employee names and finally we saved the data in my table.
 
 
 
Thanks for reading this article. Stay tuned with us for more on ASP.NET Web API.