1

This is the first time i've tried making a call to an API and I'm struggling a little. I keep getting back my error message, I plan on using the json response to populate my object. The OMDB api instructions are here (not helpful though): http://www.omdbapi.com/

private static async Task RunAsync()
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://www.omdbapi.com/?");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.GetAsync("t=Captain+Phillips&r=json").Result;

        if (response.IsSuccessStatusCode)
        {
            Console.WriteLine("Success");
        }
        else
        {
            Console.WriteLine("Error with feed");
        }
    }
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
ManxJason
  • 928
  • 12
  • 33

1 Answers1

4

You have placed the question mark (?) on the wrong place. Try like this:

private static async Task RunAsync()
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://www.omdbapi.com");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = await client.GetAsync("?t=Captain+Phillips&r=json");

        if (response.IsSuccessStatusCode)
        {
            Console.WriteLine("Success");
        }
        else
        {
            Console.WriteLine("Error with feed");
        }
    }
}

Notice that the question mark is here:

HttpResponseMessage response = await client.GetAsync("?t=Captain+Phillips&r=json");

and not on the base url as you placed it.

Also in order to properly write your asynchronous method you need to await on it inside and not be eagerly calling the .Result property which of course is a blocking operation.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Where does it say that it contacts the QueryString part to the base address. Ive looked [here](https://msdn.microsoft.com/en-us/library/hh158944(v=vs.118).aspx) but it says URI. but where is the officeal doc that says that it appends the QS to the base ? ( in GetAsync) – Royi Namir Jan 25 '15 at 13:54
  • @RoyiNamir, I am not quite sure whether there's official documentation for this. Just look with Fiddler to know what happens under the covers and the exact HTTP request that gets sent. – Darin Dimitrov Jan 25 '15 at 17:13