Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an object from the HttpResponseMessage?

I have a Post method that returns an HttpResponseMessage:

HttpResponseMessage response = 
    Request.CreateResponse(HttpStatusCode.Created, updatedItemDto);

I'm writing some tests for this and would like to get the updated item from the HttpResponseMessage (particularly the ItemId). I tried inspecting the object and it looks like the object lives in Response.Content, but I don't know how to get it from the Content.

like image 808
RobVious Avatar asked Mar 03 '13 15:03

RobVious


1 Answers

If HttpResponseMessage contain serialized data model of a known class then you can use ReadAsAsync<>() extension method like this

 MyObject obj = await response.ReadAsAsync<MyObject>();

And this is the most simple and easy way.

like image 136
Emdadul Sawon Avatar answered Nov 03 '22 09:11

Emdadul Sawon