REST Web API is the most vital component for sharing data across
multiple devices; e.g., mobile devices, desktop applications or any
website. The key element in developing & designing REST web API is
to identify the type of methods that will eventually share the data. The most popularly used method types are GET & POST.
Today, I shall be demonstrating the creation of REST Web API POST type method using ASP.NET REST Web API platform.
Prerequisites
Following are some prerequisites before you proceed any further in this tutorial:
- Knowledge of REST Web API.
- Knowledge of ASP.NET MVC5.
- Knowledge of C# Programming.
The example code is
being developed in Microsoft Visual Studio 2019 Professional.The sample sales data is taken randomly from the internet.
Let's begin now.
Step 1
Create a new Web API project and name it "RESTWebApiPostMethod".
Step 2
Create "Controllers\WebApiController.cs" file.
Step 3
Create "Post" method inside "Controllers\WebApiController.cs" file and replace the following code in it.
- #region POST api/WebApi
-
-
-
-
-
-
-
- public HttpResponseMessage Post([FromBody]JToken postData, HttpRequestMessage request)
- {
-
- HttpResponseMessage response = null;
- RequestObj requestObj = JsonConvert.DeserializeObject<RequestObj>(postData.ToString());
- DataTable responseObj = new DataTable();
- string json = string.Empty;
-
- ...
-
- ...
-
- ...
-
- json = JsonConvert.SerializeObject(responseObj);
- response = Request.CreateResponse(HttpStatusCode.OK);
- response.Content = new StringContent(json, Encoding.UTF8, "application/json");
-
- ...
-
- return response;
- }
-
- #endregion
In the above code, a simple post method has
been created which deserialize the input JSON data, then process the
request query and finally, prepare & send the response JSON packet.
There are a few things in the above code that need to be taken care of:
- The first point is the name of our target POST/GET type method. Know
that in ASP.NET MVC platform, the advantage of writing REST Web API is
that the platform take care of most of the things, the default names by
convention in ASP.NET MVC REST Web API platform are "Post" for POST type
method and "Get" for GET type method with method overloading with
parameter variations. This is the reason developers do not explicitly
need to write "HttpPost" data annotation/Attribute tag above POST type
method to explicitly tell the platform that it is POST type method.
However, if you need to change the default name of post/get method or
you have a more complex number of methods then you can utilize Method Name Routing technique to achieve your target name, but, follow the convention and use post/get as prefix with your method name.
- You should understand how you want to send your input
query data; i.e. via URL parameters or via JSON. In the above sample, I
am sending input query data via JSON structure. For complex input query
data JSON structure is recommended and for simple one or two parameters, the URL form input data structure is preferred.
- You need to deserialize your input request query and map the input request structure to a JSON object mapper if you are using JSON as input request structure.
- Finally, you need to explicitly create your
response packet to return JSON resultant data otherwise your direct JSON
response might have slashes and improper string formatting making it dificult for your consumer application to consume the REST Web API.
Step 4
Let's test out REST Web API in action using REST Web API client. I am using Firefox plugin i.e. "RESTED".
Execute the project and simply hit the REST Web API via URL directly as
the REST Web API does not have any authorization and I will get the following response:
Conclusion
In this article, you learned to create REST Web API
POST type method using ASP.NET REST Web API platform. You will also
learn about default behavior that ASP.NET MVC platform offers in respect
to creating POST/GET type methods. You learned about choosing the right
input request query type form; i.e. either via URL parameter or via JSON.
You then learned to deserialize the input request data using JSON
object mapper technique. You also learned to properly create response
packet with proper string formatting and finally, you learned to
test REST Web API using any REST client to see your REST Web API in
action before consumption.