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 based on the business requirement.
Today, I shall
be demonstrating consumption of GET type REST Web API method without any
API authorization with and without request query URL parameters using
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 GET Method solution as server side.
Let's begin now.
Step 1
Create new C#.NET Console Application project and name it "AccessGetRESTWebApi".
Step 2
Create target JSON object mappers for request/response objects as according to ASP.NET MVC - REST Web API GET Method server side solution.
Step 3
Install "Newtonsoft.Json" & "Microsoft.AspNet.WebApi.Client" NuGet libraries.
Step 4
Create "GetInfo" method without parameters in "Program.cs" file and replace the following code in it:
- ...
- public static async Task<DataTable> GetInfo()
- {
- // Initialization.
- DataTable responseObj = new DataTable();
- // HTTP GET.
- using (var client = new HttpClient())
- {
- // Setting Base address.
- client.BaseAddress = new Uri("https://localhost:44334/");
- // Setting content type.
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- // Initialization.
- HttpResponseMessage response = new HttpResponseMessage();
- // HTTP GET
- response = await client.GetAsync("api/WebApi").ConfigureAwait(false);
- // Verification
- if (response.IsSuccessStatusCode)
- {
- // Reading Response.
- 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 GET type REST Web API method without passing
any request parameters in the API URL. First I have initialized my base
url from ASP.NET MVC - REST Web API GET Method
server side solution, secondly, I have initialized content default
header as JSON type, at the third step I have called the GET type REST Web API
and finally, after successfully receiving all data from the server
because I am not passing any request query parameter, I have
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 too much of a lazy person (😁😋) to create a complex JSON mapper for my response, so, I simply used "DataTable"
structure.
Step 5
Now, create "GetInfo" method with request query parameters as a Key Value pair string in "Program.cs" file and replace the following code in it:



Hamid KhanPosted Sep 27, 2020, 10:19 AM
Good explanation..............Thanks for sharing
Carlos AriasPosted Jul 27, 2020, 5:55 PM
Gracias, genial ejemplo me sirvio mucho ya que a alguien se le ocurrio pedir datos en un encabezado.