In this article, we are going to learn how to consume the ASP.NET Core WEB API GET Method in the .Net Core Console Application in simple steps.

Let’s get started with New DOTNET SHORTS Series.

We already have a GET API which is developed in ASP.Net Core we are going to consume this API in the .Net Core Console Application.

GET API which we are going to consume.

This snapshot is from the ASP.NET CORE WEB API GET Method.

Swagger View of APIs which we are going to consume.

Swagger view of APIs which is developed in ASP.NET CORE which we are going to consume in a console application.

Execution of API in Postman.

After calling GET Music API, it will return a list of Songs.

For Consuming APIs, we have created a .Net Core Application which Target Framework .Net 6.0 with the name ConsoleConsumer.

Accept Header: – Accept Header tells the API that it is expecting the response in the specified media type.

Steps to Consume GET API

  1. HttpClient class instance for sending HTTP Requests.
  2. Setting Base Address “https://localhost:44373”
  3. Setting Accept Header “application/json”
  4. Adding a class with the name SongModel which has properties and fields of response json.
  5. Next, we are calling GetAsync Method and Passing Request Uri (“/api/Music”) to it which will send a GET request to the specified Uri in an asynchronous way.
  6. Using Newtonsoft.Json Nuget package for DeserializeObject (response we are going to receive will be Json format which we need to map to .Net Type)

Adding Class SongsModel

SongModel class has properties which are the same as json response string which we going to receive as a response.

Json which we are going to receive as a Response.

        "SongId": 1,
        "Name": "Dynamite",
        "Length": "3:19",
        "Yearreleased": 2020,
        "Writer": "David Stewart Jessica Agombar",
        "Singer": "BTS",
        "Genre": "Pop music",
        "Rating": 5000
 public class SongsModel
        public int SongId { get; set; }
        public string Name { get; set; }
        public string Length { get; set; }
        public int Yearreleased { get; set; }
        public string Writer { get; set; }
        public string Singer { get; set; }
        public string Genre { get; set; }
        public int Rating { get; set; }

Method for Consuming API

In this method, we are going to set BaseAddress https://localhost:44373 along with Accept Headers as application/json and call GetAsync (“GET Method”) to the GetAsync() method we are going to pass the request, Uri.

As this method GetAsync() will get called we are going to receive a response and in this response, the first thing we are going to do is check the Status of it if it is a success then we are going to read the response and then Deserialize it.

 static async Task GetJsonData()
            using var client = new HttpClient();
            client.BaseAddress = new Uri("https://localhost:44373");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync("/api/Music");
            if (response.IsSuccessStatusCode)
                var responseBody = await response.Content.ReadAsStringAsync();
                    var songs = JsonConvert.DeserializeObject<List<SongsModel>>(responseBody);
                    if (songs != null)
                        foreach (var song in songs)
                            Console.WriteLine($"Name: {song.Name},Year: {song.Yearreleased}");
                catch (JsonReaderException)
                    Console.WriteLine("Something went wrong.");

Calling GetJsonData Method in Main() Method

 static void Main(string[] args)
            Console.WriteLine("Hello, World!");

View of runtime receiving a response

Json object which we have received in response

Below snapshot after the deserializing Json object

In this article, we have learned how to consume the GET method in the .Net Core Console Application in simple steps with sample code.

Welcome my New Dotnet Shorts #DOTNETSHORTS Series Hope you like it small and simple.

By Saineshwar

Microsoft MVP for Developer Technologies | C# Corner MVP | Code project MVP | Senior Technical Lead | Author | Speaker | Love .Net | Full Stack developer | Open source contributor.