Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Get data from Rest WebAPI in c#

Consume Web API data using HttpClient in c#

HttpClient is a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.

Following code example get movies data in JSON format,

Web API code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Threading;
using System.Net.Http.Headers;
public class RestAPICall
    {
        private const string URL = "https://c4utest.com/api/movies/search";
        private const string urlParameters = "?Title=Bahubali ";
     
       public void  DisplayMoviesFromRestAPI()
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(URL);
                // List data response.
                HttpResponseMessage response = client.GetAsync(urlParameters).Result; 
                // blocking call
                if (response.IsSuccessStatusCode)
                {
                    // Parse the response body.
                    var dataObjects = response.Content.ReadAsStringAsync().Result; 
                    //data objects
                    Movies deserializedProduct = JsonConvert.DeserializeObject<Movies>(dataObjects);
                    JObject jsonval = JObject.Parse(dataObjects);
                    if (deserializedProduct != null)
                    {
                        Console.WriteLine("Total movies : "+ deserializedProduct.total);
                        Console.WriteLine("Current page : " + deserializedProduct.page);
                        Console.WriteLine("Total pages : " + deserializedProduct.total_pages);
 
                        //first movie details
                        var firstMoviesDetails = deserializedProduct.data.FirstOrDefault();
                        //Display first moview details from collections
                        Console.WriteLine("First movie title : " + firstMoviesDetails.Title);
                        Console.WriteLine("First movie release year : " + firstMoviesDetails.Year);
                        Console.WriteLine("First movie code : " + firstMoviesDetails.imdbID);
 
                        //display all movies details
                        foreach (var movieDetail in deserializedProduct.data)
                        {
                            Console.WriteLine("Movie title : " + movieDetail.Title);
                            Console.WriteLine("Movie release year : " + movieDetail.Year);
                            Console.WriteLine("Movie code : " + movieDetail.imdbID);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                }
            }
        }
    }

Movies class

public class Movies
    {
      public int page { get; set; }
      public int per_page { get; set; }
      public int total { get; set; }
      public int total_pages { get; set; }
      public List<MoviesData> data { get; set; }
    }

MoviesData class 

public class MoviesData
    {
        public string Title { get; set; }
        public int Year { get; set; }
        public string imdbID { get; set; }
    }