How to implement MVC REST Api to Add Records

In previous post How to implement GET method with MVC REST WebApi, I discussed the very basic implementation of a WebApi using MVC. That post covered R of CRUD showing how you can implement method to GET data. In this post I will talk about implementation of POST method that allows you to create new objects. Yes, in strict implementation of a REST API, sending a POST request is a hint to framework to look for a method that is prefixed with Post. Therefore extending the example from previous post where I created a Seller controller to manage seller accounts in my trading system, I need a method that looks like PostSeller. This means when a client send a HTTP POST request to my API, MVC framework will route that request to this method.

REST Api Post Method

Following code snippet shows how I implemented method in my REST API to handle request to add new sellers in my trading system.

public HttpResponseMessage PostSeller(Seller seller)
   HttpResponseMessage response = new HttpResponseMessage();
     response.StatusCode = System.Net.HttpStatusCode.Created;
     response.Headers.Location = new Uri(Url.Link("Sellers", new { id = seller.Id }));
   catch (Exception ex)
      response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
      //TODO: Log this message.
   return response;

You can see from the signature of this method that emphasis is on POST part of the name.

.Net client to call REST API for POST

Now that we have our REST service that implements POST method to add new records, let's see how a .Net client calls this API to send data to post new record.

public JsonResult UpdateSeller(Models.SellerModel model)
    var responseMessage = 
        new Models.PostResponseMessage() 
        { StatusCode = HttpStatusCode.InternalServerError };
    if (!ModelState.IsValid)
      responseMessage.StatusCode = HttpStatusCode.BadRequest;
      responseMessage.Message = "Invalid data";
      responseMessage.Error = "Invalid data";
      Json(responseMessage, JsonRequestBehavior.AllowGet);

    var gatewayClient = 
    HttpResponseMessage resp = null;
      resp = gatewayClient.PostAsJsonAsync("api/sellers", model.ToSeller()).Result;
      responseMessage.StatusCode = resp.StatusCode;
    catch (HttpRequestException reqEx)
    catch (Exception ex)

    return Json(responseMessage);

The client uses PostAsJsonAsync method. This method constructs HTTP request with POST as method type. It then serializes the data passed to this method as JSON object into body of the request and sends the request over to REST API. And then API operates on the data and send the response back as JSON object.

As you can see from this discussion how easy it is to implement a REST API method that will be called by client to add new records. This covers C of CRUD operations. In upcoming posts I will discuss the remaining methods that can be used to update and delete data using appropriate HTTP request method types.




23.6 °C / 74.6 °F

weather conditions Clouds

Monthly Posts

Blog Tags