Web API with POST Method

In previous article I have explain HTTP Get method in ASP.Net web API.

Web API with GET Method

In this blog I will explain HTTP Post Method.

SNO Action HTTP Method Relative URI
1. Create New Employee POST /api/Employee

Creating Resources HTTP POST

Note: Method name should be start from Post.

Insert new record in list.

This method add new employee in list and create response with success status and create URI for new record and add in header of response.

/// <summary>
///
This Method add new record in list of employee.
/// and create response and allocate uri with id in header of response.
/// </summary>
///
<param name="emp"></param>
/// <returns></returns>
public HttpResponseMessage PostAddNewEmployee(Employee emp)
{

     if (emp == null)
     {
         
throw new ArgumentNullException();
     }

    _emp.Add(emp);
     
var response = Request.CreateResponse<Employee>(HttpStatusCode.Created, emp);
     
string uri = Url.Link("DefaultApi", new { id = emp.Uid });
     response.Headers.Location =
new Uri(uri);
      
return response;
}

Now In this step I will explain how to consume HTTP service Web API Post Method in Dot net.

Create a Resource (HTTP POST)

In this method set base address of Asp.Net Web API and sets the accept header to application/json that tells the server to send data in JSON Format.

PostAsJsonAsyn:This method serializes object into JSON format and send POST request to.

After that this method return Response object.

private static HttpResponseMessage ClientPostRequest(string RequestURI,Employee emp)
{
        
HttpClient client = new HttpClient();
        client.BaseAddress =
new Uri("http://localhost:1379/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
        
HttpResponseMessage response = client.PostAsJsonAsync(RequestURI,emp).Result;
        
return response;
}

Add This code to Main function of program.cs.->Add New Record in List of employee.

In this method create new employee and call above created method and pass employee object as a parameter then receive response from this method then call EnsureSuccessStatusCode method –This method throw exception when response status in not success.

After that check IsSuccessStatusCode true and false and return URL with Newly created employee ID.

//HTTP POST
Console.WriteLine("----------------Add New Employee -------------------");
Employee emp=new Employee(){ Uid=6,Name="Sikha",Address="Agra",City="agra"};
HttpResponseMessage responsePostMethod = ClientPostRequest("api/Employee",emp);
responsePostMethod.EnsureSuccessStatusCode();
if (responsePostMethod.IsSuccessStatusCode)
{
      
Uri empUrl = responsePostMethod.Headers.Location;
      
Console.WriteLine(empUrl.ToString());
}

After that you will run your application and see output.

output

In Next article I will explain how to update Existing record using INSERT-PUT Method of HTTP.

Thank You for reading my article.
Ebook Download
View all
Learn
View all