title: How to read webapi responses with HttpClient in C#
body: <p>I have developed a small webapi which has a few actions and returns my custom class called <code>Response</code>.</p>
  <p>The <code>Response</code> class</p>
  <pre><code>public class Response
  {
      bool IsSuccess=false;
      string Message;
      object ResponseData;
  
      public Response(bool status, string message, object data)
      {
          IsSuccess = status;
          Message = message;
          ResponseData = data;
      }
  }
  </code></pre>
  <p>My webapi with actions</p>
  <pre><code>[RoutePrefix(&quot;api/customer&quot;)]
  public class CustomerController : ApiController
  {
      static readonly ICustomerRepository repository = new CustomerRepository();
  
      [HttpGet, Route(&quot;GetAll&quot;)]
      public Response GetAllCustomers()
      {
          return new Response(true, &quot;SUCCESS&quot;, repository.GetAll());
      }
  
      [HttpGet, Route(&quot;GetByID/{customerID}&quot;)]
      public Response GetCustomer(string customerID)
      {
          Customer customer = repository.Get(customerID);
          if (customer == null)
          {
              throw new HttpResponseException(HttpStatusCode.NotFound);
          }
          return new Response(true, &quot;SUCCESS&quot;, customer);
          //return Request.CreateResponse(HttpStatusCode.OK, response);
      }
  
      [HttpGet, Route(&quot;GetByCountryName/{country}&quot;)]
      public IEnumerable&lt;Customer&gt; GetCustomersByCountry(string country)
      {
          return repository.GetAll().Where(
              c =&gt; string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase));
      }
  }
  </code></pre>
  <p>Now where I am stuck is that I do not know how to read the response data returned from the webapi actions and extract json from my response class. After getting json how could I <code>deserialize</code> that json to the customer class.</p>
  <p>This is the way I am calling my webapi function:</p>
  <pre><code>private void btnLoad_Click(object sender, EventArgs e)
  {
      HttpClient client = new HttpClient();
      client.BaseAddress = new Uri(&quot;http://localhost:8010/&quot;);
      // Add an Accept header for JSON format.  
      //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
      // List all Names.  
      HttpResponseMessage response = client.GetAsync(&quot;api/customer/GetAll&quot;).Result;  // Blocking call!  
      if (response.IsSuccessStatusCode)
      {
          Console.WriteLine(&quot;Request Message Information:- \n\n&quot; + response.RequestMessage + &quot;\n&quot;);
          Console.WriteLine(&quot;Response Message Header \n\n&quot; + response.Content.Headers + &quot;\n&quot;);
      }
      else
      {
          Console.WriteLine(&quot;{0} ({1})&quot;, (int)response.StatusCode, response.ReasonPhrase);
      }
      Console.ReadLine();   
  }
  </code></pre>
  <h5>Questions</h5>
  <ol>
  <li><p>How to get the response class the webapi returns at the client side</p>
  </li>
  <li><p>How could I extract json from the response class</p>
  </li>
  <li><p>How to deserialize the json to the customer class at client side</p>
  </li>
  </ol>
  <hr />
  <p>I use this code but still getting an error.</p>
  <pre><code>    var baseAddress = &quot;http://localhost:8010/api/customer/GetAll&quot;;
      using (var client = new HttpClient())
      {
          using (var response =  client.GetAsync(baseAddress).Result)
          {
              if (response.IsSuccessStatusCode)
              {
                  var customerJsonString = await response.Content.ReadAsStringAsync();
                  var cust = JsonConvert.DeserializeObject&lt;Response&gt;(customerJsonString);
              }
              else
              {
                  Console.WriteLine(&quot;{0} ({1})&quot;, (int)response.StatusCode, response.ReasonPhrase);
              }
          }
      }
  </code></pre>
  <p>The error is:</p>
  <blockquote>
  <p>An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code</p>
  <p>Additional information: Cannot deserialize the current JSON object (e.g. {&quot;name&quot;:&quot;value&quot;}) into type 'WebAPIClient.Response[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.</p>
  </blockquote>
  <p>Why is the response causing this error?</p>