Sunday 23 May 2021

Part 10 : Consuming API from Console Application

#5 Minutes Of Reading

In this article, I will show you how to consume web API from console application and asp.net core web application in upcoming article. I’m using web API project created in Part 7 of Web API series and code available here.
I have two API method Get and Get with specific Id shown below and consuming below methods in this article.
Create core Console application ConsumingRest_API and create class Users under it with below piece of code.
Above method from class Users under console application is responsible to call Get method of API.
HTTPClient is class from System.Net.Http namespace responsible to call web api method in console application. To call API method HttpClient expect URL of web api hosted on web server, Media type (Accept is a request header value for informing server what format client support) in my case I choose application/json.
Client.GetAsync() method call Get method and I’m reading data in string, you can also get it in complex type which I will show you in later article.
I shown you how to call Get method of API which return list of users from API, let’s see how to call Get method with specific Id.
Above two methods from Users class is ready to consume API methods. Below piece of code call methods from Users and show API response on console.
In upcoming article I will show you how to consume Web API in web application. I will also walk you through how to consume API with Bearer Token in upcoming articles.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home