1

I'm trying to teach myself how to make simple API calls with C#. I'd like to call this "http://hivemc.com/json/userprofile/da10b68dea6a42d58ea8fea66a57b886". This should return some strings in json but I don't know what i'm supposed to do with that.

reference: https://apidoc.hivemc.com/#!/GameData/get_game_game_data_id_UUID

I'm new to programming and I've never done anything with API's. I've tried looking around the internet but I don't understand what I'm supposed to look for. Can someone refer me to an article that can teach me how to do this? I have no idea where to start. An example of the code with explanation would be great but I understand if it's too much to ask.

Thank you!

  • Use System.Net.HttpClient and look at the Newtonsoft.JSON nuget package. – ProgrammingLlama Mar 29 '17 at 09:40
  • Check [this](http://www.newtonsoft.com/json). Everything about Json is there. – mrogal.ski Mar 29 '17 at 09:40
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Smartis has left SO again Mar 29 '17 at 09:43

2 Answers2

3

You can start from the following.

using System;
using System.Net.Http;
using System.Threading.Tasks;

class Test
{
    public static void Do()
    {
        var result = GetGameData("da10b68dea6a42d58ea8fea66a57b886").Result;

        //TODO parse json here. For example, see http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c

        Console.WriteLine(result);
    }

    private static async Task<string> GetGameData(string id)
    {
        var url = "http://hivemc.com/json/userprofile/" + id;

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(url);

            HttpResponseMessage response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                string strResult = await response.Content.ReadAsStringAsync();

                return strResult;
            }
            else
            {
                return null;
            }
        }
    }
}

Sample call

Test.Do();
husky
  • 87
  • 5
1

You should use System.Net.HttpClient from Nuget. Check this link out. It shows you how to get data from the API. The next step is to deserialize it to your model using Newtonsoft.Json.

Hope it helps!

mindOfAi
  • 4,412
  • 2
  • 16
  • 27