-2

I'm pretty new to C# and this is my first time getting data from an api. I was wondering how do I get or call the data collected in this api request (MakeRequest). Preferably assign the data to a public string. The data from the api request is in json format.

using System;
using System.Net.Http.Headers;
using System.Text;
using System.Net.Http;
using System.Web;

namespace CSHttpClientSample
{
    public partial class Form1 : Form
    {         
        public async void MakeRequest()
        {
            var client = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{subscription key}");

            // Request parameters
            queryString["seasonId"] = "{string}";
            var uri = "https://www.haloapi.com/stats/{title}/servicerecords/arena?players={players}&" + queryString;

            var response = await client.GetAsync(uri);
        }
    }
}
Jose Cancel
  • 151
  • 1
  • 2
  • 15
  • first of all, `async void` is not a good idea. second: what is your problem with that code? What do you want to achieve? And what did you try? – Waescher Mar 05 '16 at 00:14
  • I wish to collect json data from that api request. – Jose Cancel Mar 05 '16 at 00:15
  • so your respose is likely to be the JSON string you downloaded. there you have it. so you "collected" it if you will. but I suppose you want to deserialize that JSON string to any business objects, right? – Waescher Mar 05 '16 at 00:16
  • I'm pretty new to c# and coding in general. This code was provided and I'm not sure where it's downloading the json string. I wish go access the gathered json data. – Jose Cancel Mar 05 '16 at 00:19
  • Possible duplicate of [deserializing JSON to .net object using NewtonSoft (or linq to json maybe?)](http://stackoverflow.com/questions/4749639/deserializing-json-to-net-object-using-newtonsoft-or-linq-to-json-maybe) – Jasen Mar 05 '16 at 00:28
  • I think he is not at deserializing yet but looking for the JSON he downloaded – Waescher Mar 05 '16 at 00:29

1 Answers1

1

So if the call succeeded, you should have your JSON string returned in the response variable you assign in the last line.

Use your debugger and inspect that variable. If you look at the MSDN doc for your GetAsync() method (Link), you can easily find out that the variable is of the type HttpResponseMessage. This class has an own page here telling you that there's a property Content.

This is your JSON string, now might come the part where you have to do some deserializing. Have fun.

Waescher
  • 5,361
  • 3
  • 34
  • 51