9

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.

rae1
  • 6,066
  • 4
  • 27
  • 48
RobVious
  • 12,685
  • 25
  • 99
  • 181
  • Can you access the [Content property](http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage.content.aspx)? What is the return being use for? – rae1 Mar 03 '13 at 16:49
  • Yes I can. The return is in a test which will take the Item's ItemID and update some values. – RobVious Mar 03 '13 at 17:16
  • Because .Content is not an – RobVious Mar 03 '13 at 17:52
  • Exactly what I was looking for. Thanks. Would you mind putting this in an answer so I can check it off? – RobVious Mar 03 '13 at 18:00

2 Answers2

5

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.

Emdadul Sawon
  • 5,730
  • 3
  • 45
  • 48
4

You can inspect the response in the debugger since you mention "it looks like the object lives in Response.Content".

You might need to cast it to something meaningful, like in this response,

Request.CreateResponse(HttpStatusCode.Created, updatedItemDto) as MyObject;
Community
  • 1
  • 1
rae1
  • 6,066
  • 4
  • 27
  • 48