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");
        }
    }
}
1
  • 1
    What error are you getting? Commented Jan 25, 2015 at 13:40

1 Answer 1

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.

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

2 Comments

Where does it say that it contacts the QueryString part to the base address. Ive looked here but it says URI. but where is the officeal doc that says that it appends the QS to the base ? ( in GetAsync)
@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.

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.