0

Im new to System.Net with C# I want a way to get info from this website api: https://fn-api.glitch.me/api/aes from its json to a C# string

I have this so far

I don't know how to get each item and where to put the url (im really new to this).

I want the url in a string:

public class Data
{
    public string build { get; set; }
    public string netCL { get; set; }
    public string manifestID { get; set; }
    public string aes { get; set; }
}

public class RootObject
{
    public Data data { get; set; }
}
Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
Kye
  • 76
  • 1
  • 10
  • "I want the url in a string" : `string url = "https://fn-api.glitch.me/api/aes";`. Maybe you want to get the content of the page ? Take a look at [System.Net.WebClient](https://learn.microsoft.com/fr-fr/dotnet/api/system.net.webclient?view=netframework-4.8)(You didn't say if you are on .net core or .net full framework – Steve B Feb 14 '20 at 07:55

3 Answers3

1

Okay, this is how you get about it. I am showing you an example using HttpClient to first read the content from the API and then de-serialize it using Newtonsoft package.

HttpClient class:

public class HttpClientFactory
{
  private string webServiceUrl = "https://fn-api.glitch.me/";

  public HttpClient CreateClient()
  {
    var client = new HttpClient();
    SetupClientDefaults(client);
    return client;
  }

  protected virtual void SetupClientDefaults(HttpClient client)
  {
    //This is global for all REST web service calls
    client.Timeout = TimeSpan.FromSeconds(60);
    client.BaseAddress = new Uri(webServiceUrl);
  }
}

Your Model class:

public class Data
{
  public string build { get; set; }
  public string netCL { get; set; }
  public string manifestID { get; set; }
  public string aes { get; set; }
}

public class RootObject
{
  public Data data { get; set; }
}

Now, you can call this class and create an instance of the HttpClient like this:

public RootObject InvokeAPI()
{
  RootObject apiresponse = new RootObject();
  string result = string.Empty;
  HttpClientFactory clientFactory = new HttpClientFactory();
  var client = clientFactory.CreateClient();
  HttpResponseMessage response = client.GetAsync("api/aes").Result;
  if (response.IsSuccessStatusCode)
  {
    result = response.Content.ReadAsStringAsync().Result;
    apiresponse = JsonConvert.DeserializeObject<RootObject>(result);
  }
 return apiresponse;
}

Hope this helps you out.

EDIT:

As per your code, you need to call the API on your Button click:

    private void metroButton2_Click_1(object sender, EventArgs e)
    {
        //You need to invoke the API method !!!!
        var apiresponse=InvokeAPI();
        metroTextBox1.Text = apiresponse.data.aes;
    }

Be sure to put try-catch blocks on your code for error handling.

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
  • This may be a silly question but how do I GET the aes? Like if I wanted to do this: `metroTextBox1.Text = ???;` – Kye Feb 14 '20 at 08:39
  • @ProMasterBoy-Gaming Once you get the de-serialized object, you can do: `metroTextBox1.Text=apiresponse.data.aes;` – Rahul Sharma Feb 14 '20 at 08:41
  • @ProMasterBoy-Gaming You are de-serializing the string here: `apiresponse = JsonConvert.DeserializeObject(result);` where apiresponse is of type `RootObject` from your `Model` – Rahul Sharma Feb 14 '20 at 08:55
  • Where would I put the `apiresponse = JsonConvert.DeserializeObject(result);`? – Kye Feb 14 '20 at 08:59
  • @ProMasterBoy-Gaming I am not quite sure about that since I do not have your code structure here. You would need to put this where you are calling the `API` via `HttpClient` – Rahul Sharma Feb 14 '20 at 09:05
0

I'd recommend using a 3rd party library like RestSharp. It'll give you a client that's easy to work with and does the converting into objects automatically.

Alternatively you could use the WebClient and download the JSON. Using something like Json.NET allows you to deserialize the JSON into an object.

mboldt
  • 1,780
  • 11
  • 15
0
  1. Easiest way to read from a URL into a string in .NET
  2. I use JSON.Net.