Correct way to use HttpClient with JSON responses

Topics: Web Api
Jun 22, 2011 at 10:01 AM

Hello,

How does one ask HttpClient to deserialize the response using JSON.NET? The code below seems to fail because ObjectContent.HttpResponseMessage never gets set.

namespace ConsoleApplication1
{
    class Test
    {
        public string Foo { getset; }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            HttpClient client = new HttpClient("http://localhost:8000");
 
            var resp = client.Get("Test");
            Console.WriteLine(resp.Content.ReadAs<Test>(new[] { new JsonMediaTypeFormatter() }).Foo);
        }
    }
}

Thanks