Wednesday 24 March 2021

Part 5: API Call using Postman and HTTP Client

#5 minutes of reading In this article I will show you calling API using postman tool and consuming API in other application like asp.net core. Web API can be consumed in any application like asp.net core, console application etc with the help of HTTP Client.
Once postman downloaded, select get request and paste URL highlighted below after Get request dropdown.
Click on Send button and API response data/information loaded under Body section.
  Now lets consume the API from asp.net core 3.1 web application, create asp.net core web application template shown below. Source code available here
Provide name for an application.
  Select .net Core framework SDK 3.1
  Project gets created now Open Startup.cs file. By default startup file comes with default code shown below. I have explain about startup.cs file in my asp.net core web application series for more details please visit here.
As per below screen update ConfigureServices method in startup.cs file. Highlighted line of code is registering Http Client service for core web application.
  Create Model class with name WeatherForecast and properties. It’s a user defined data type need to create for holding data returned from web API.
Create LoadData action method, I have injected IHttpCLientFactory in constructor (dependency injection). By default core provide dependency injection.
LoadData method call WeatherForecast API Get method. For consuming API I’m using IHttpClientFactory interface which create instance of Http Client using CreateClient method written in line number 30.
Once instance created need to provide MediaType like information/data read in Json or XML format.
  Create View for LoadData method and add below piece of code in LoadData.cshtml view to render api data here.
Let’s run application and check browser window for output.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home