Section 6: Adding a POST (Restful API in ASP .Net Core)

 Section 6: Adding a POST (Restful API in ASP .Net Core)


Introduction:

In the Section 5, we have seen how we can add a HTTP GET method to get the data from the server/API. In this section, let us see how to add a POST method to create a record.


To add a POST method,


  • Create a new method Post().
  • Post() method should return a list of Movies (model). Hence, let us return IEnumerable<Movie>.
  • Post() method will accept a model as Parameter. In our case, it is Movie model. The parameter should have [FromBody], which says that the request data(param) is available in the body of the HTTP Request.
  • Add a Data Attribute, [HttpPost] to this method.

Let us see this in action:





To test this,

  • Open Postman.
  • Create a request and select POST. 
  • Add a JSON in the Body of the request.
  • Send the request.