Data communication is one of the most vital components when working on
client
machines, mostly to access/store sensitive data onto cloud servers. Any
method type of POST or GET of REST Web API can be used for data
communication between client machines and cloud servers base on the business
requirement.
Today, I shall be
demonstrating consumption of POST type REST Web API method without any
API authorization using the ASP.NET REST Web API platform.
Prerequisites
Following are some prerequisites before you proceed any further in this tutorial:
- Understanding of JSON Object Mapper.
- 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. I have used
ASP.NET MVC - REST Web API POST Method solution as server side.
Let's begin now.
Step 1
Create new C#.NET Console Application project and name it "AccessPostRESTWebApi".
Step 2
Step 3
Install "Newtonsoft.Json" & "Microsoft.AspNet.WebApi.Client" NuGet libraries.
Step 4
Create "PostInfo" method in "Program.cs" file and replace the following code in it:
- ...
- public static async Task<DataTable> PostInfo(RequestObj requestObj)
- {
-
- DataTable responseObj = new DataTable();
-
-
- using (var client = new HttpClient())
- {
-
- client.BaseAddress = new Uri("https://localhost:44371/");
-
-
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
-
-
- HttpResponseMessage response = new HttpResponseMessage();
-
-
- response = await client.PostAsJsonAsync("api/WebApi", requestObj).ConfigureAwait(false);
-
-
- if (response.IsSuccessStatusCode)
- {
-
- string result = response.Content.ReadAsStringAsync().Result;
- responseObj = JsonConvert.DeserializeObject<DataTable>(result);
- }
- }
-
- return responseObj;
- }
- ...
In the above code, I am using "HttpClient"
library to consume/access POST type REST Web API method. The code is
quite straightforward; i.e., first I have initialized my base url from
ASP.NET MVC - REST Web API POST Method
server side solution, secondly, I have initialized content default
header as JSON type, and in the third step I have posted my request object and
called the POST type REST Web API. Finally, after successfully receiving a database on my request query data, I deserialized the response into my
target object mapper. Notice that I have used "DataTable" structure to
map my response data. I could have created a target complex JSON object
mapper then deserialized it, but, instead I have used "DataTable". You
can use either option, since my response JSON object is complex
and I am a lazy person (😁😋) so, I simply used "DataTable" structure to create a complex JSON mapper for my response.
Step 5
In "Program.cs" file "Main" method write the following line of code to call the POST type REST Web API method:
- ...
-
- RequestObj requestObj = new RequestObj { Priority = "M", SalesChannel = "online" };
-
-
- DataTable responseObj = Program.PostInfo(requestObj).Result;
- ...
In the above lines of code, I am simply
calling my POST type REST web API with input request query data and
storing my response into a "DataTable" structure.
Step 6
If you execute the provided solution, you will be able to see the following:
Conclusion
In this article, you learned to consume POST type
REST Web API method without any API authorization using ASP.NET REST Web
API platform. You also learned to utilize "HttpClient" library to
consume REST Web APIs and finally, you learned about desiralizing
REST web API response directly into "DataTable" structure.