Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract contents from HttpResponseMessage

I'm recieving an object of type System.Net.Http.HttpResponseMessage<List<T>>, how do I get the List<T>?

I tried casting the content property and getting a value from the content property via its value property but nothing seems to work.

Thanks for the help!

like image 637
Nicolás Straub Avatar asked Dec 27 '11 22:12

Nicolás Straub


People also ask

How do I return content in HttpResponseMessage?

Several months ago, Microsoft decided to change up the HttpResponseMessage class. Before, you could simply pass a data type into the constructor, and then return the message with that data, but not anymore. Now, you need to use the Content property to set the content of the message.

What does HttpResponseMessage return?

A HttpResponseMessage allows us to work with the HTTP protocol (for example, with the headers property) and unifies our return type. In simple words an HttpResponseMessage is a way of returning a message/data from your action.

Why use HttpResponseMessage in Web API?

HttpResponseMessage. If the action returns an HttpResponseMessage, Web API converts the return value directly into an HTTP response message, using the properties of the HttpResponseMessage object to populate the response. This option gives you a lot of control over the response message.


2 Answers

This is no longer possible. Check this out: http://aspnetwebstack.codeplex.com/discussions/350492

like image 174
lnaie Avatar answered Oct 27 '22 21:10

lnaie


You can use the .ReadAsAsync<List<T>> method of the Content property.

Please also read my sample from this answer.

Update: These extension methods are located in the System.Net.Http.Formatting namespace inside the NuGet Package Microsoft.AspNet.WebApi.Client.

like image 41
Alexander Zeitler Avatar answered Oct 27 '22 22:10

Alexander Zeitler