0

This is my first exposure to working with the HttpClient.

I can see the status codes that come back from the rest call, but I'm unsure how can I read the json object that is returned from the GetData method?

    public void MyTest()
    {
        using (HttpClient httpClient = new HttpClient())
        {
            httpClient.BaseAddress = new Uri(_uri);

            var response = httpClient.GetAsync("API/GetData");
        }
    }
Nkosi
  • 235,767
  • 35
  • 427
  • 472
John Doe
  • 3,053
  • 17
  • 48
  • 75

1 Answers1

0
public async Task MyTest() {
    using (HttpClient httpClient = new HttpClient()) {
        httpClient.BaseAddress = new Uri(_uri);

        var response = await httpClient.GetAsync("API/GetData");

        if(response!=null && response.IsSuccessStatusCode) {
            var json = await response.Content.ReadAsStringAsync();
        }
    }
}

the json variable will hold the string representation of the JSON. from there you can use it as needed.

also take a look at http://www.newtonsoft.com/json/help/html/deserializeobject.htm so that you can use strongly typed instances of the json

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • possible duplicate: http://stackoverflow.com/questions/15317856/asp-net-mvc-posting-json/15318612#15318612 OR http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – Glenn Ferrie Nov 03 '16 at 13:16
  • Thank you. That worked, but opened up a new challenge on working with async methods. – John Doe Nov 03 '16 at 14:31
  • There is a lot of information in SO about that topic. – Nkosi Nov 03 '16 at 14:33