I am new to C#
and I am trying to work out how to store my HTTPResponseMessage
in an object.
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://api.themoviedb.org/3/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("search/tv?api_key=MY_KEYa&query=hawaii%20five%20o&first_air_date_year=2010");
if (response.IsSuccessStatusCode)
{
// What to do here..?
}
}
}
I used this site (http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client) for the above code.
Using this website (http://json2csharp.com/), I created the following class:
class TVShowDTO
{
public string backdrop_path { get; set; }
public int id { get; set; }
public string original_name { get; set; }
public string first_air_date { get; set; }
public List<string> origin_country { get; set; }
public string poster_path { get; set; }
public double popularity { get; set; }
public string name { get; set; }
public int vote_average { get; set; }
public int vote_count { get; set; }
}
API Response:
{
page: 1
results: [1]
0: {
backdrop_path: "/mAXpHFDTMHvJt7WdibFWdbRsdCG.jpg"
id: 32798
original_name: "Hawaii Five-0"
first_air_date: "2010-09-20"
origin_country: [1]
0: "US"
-
poster_path: "/hO4BgEJhGIrFJ7f00sR6ZcdNB6y.jpg"
popularity: 1.06099324148627
name: "Hawaii Five-0"
vote_average: 7.3
vote_count: 6
}-
-
total_pages: 1
total_results: 1
}
Here is a link to the API
I am trying to access: http://docs.themoviedb.apiary.io/reference/search/searchtv/get
Do I need to get the response
as a string
first? Because the examples I have seen of this on SO, seem to use an input string
.
I haven't been able to work this out as when I print response.Content
, it just returns "System.Net.Http.StreamContent"
Any guidance with this would be greatly appreciated.
First, you'll want to get a string of the response:
Then, you can use a tool like Json.NET from NUGET to deserialize to your object: