0

I'm new to APIs, so I've been trying to use a web request to GET information from Reddit, since the API is unlocked. I've been able to use the right URL to get information using a REST client extension, but I want to implement the body of the data and simply just print it out to a web page.

I know this is easier with python, but they use C#/ASP.NET at my work. I've looked off of tutorials such as:

http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

I was only able to obtain a header when I used this tutorial and my code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace API_TESTING
{
    class Product
    {
        public string Name { get; set; }
        public double Price { get; set; }
    }
    class Program
    {
    static void Main()
    {
        RunAsync().Wait();
    }

    static async Task RunAsync()
    {
        using(var client = new HttpClient())
        {
            //TODO - send HTTP requests
            client.BaseAddress = new Uri("http://www.reddit.com/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //-----
            HttpResponseMessage response =  await client.GetAsync("r/todayilearned/new.json");
            if(response.IsSuccessStatusCode)
            {
                Console.Write(response);

            }

        }
    }
}
}

Can someone explain how to use ASP.NET for API calls? or link to other up-to-date tutorials? Thank you.

2
  • 1
    What is your issue. Are you hitting an exception? Is the output different from what you expect? Include sample actual output and expected output. You should include that information in the question, rather than us having to ask you for it. Commented Jul 10, 2014 at 17:46
  • I mentioned I was only getting the header as my output. I guess I didn't make it clear I was aiming to get the body/json of the web API GET call. I am sorry. Commented Jul 10, 2014 at 21:59

1 Answer 1

2

You're almost there. After you get the response, you need to read its content.

var response = await _client.GetAsync(uri);
if (response.IsSuccessStatusCode) {
    var result = await response.Content.ReadAsStringAsync();
    Console.WriteLine(result);
}

You might want to check out the RedditSharp project to see how others have done this. Btw, the tutorial you linked to does almost exactly what I answered with under the Getting a Resource (HTTP GET) section. Only difference is they used the generic ReadAsAsync while you can use ReadAsStringAsync if you're just writing the body to the console.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Would the program work similarly on a webpage?
@user3704986 Anywhere you use it from c# the above would work. I would recommend moving all Reddit code out to a separate project/s so you can reuse it. That way you write it once and the same code works with your console app and your web app. Instead of writing the result to the console, just return the content as a string so different clients can react to the response in their own way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.