Getting content from HttpResponseMessage for testing using c# dynamic keyword
.ReadAsAsync<T>
is an asynchronous method, meaning that it doesn't return the whole deserialized object but a Task<T>
to handle the continuation of the whole asynchronous task.
You've two options:
1. Async pattern.
Use the async
keyword in your enclousing method (for example: public async void A()
) and do the asynchronous call this way:
dynamic responseContent = await response.Content.ReadAsAsync<object>();
string returnedToken = responseContent.Token;
2. Regular task API
Or just use the Task API:
response.Content.ReadAsAsync<object>().ContinueWith(task => {
// The Task.Result property holds the whole deserialized object
string returnedToken = ((dynamic)task.Result).Token;
});
It's up to you!
Update
Before you posted the whole screenshot, no one could know that you're calling task.Wait
in order to wait for the async result. But I'm going to maintain my answer because it may help further visitors :)
As I suggested in a comment to my own answer, you should try deserializing to ExpandoObject
. ASP.NET WebAPI uses JSON.NET as its underlying JSON serializer. That is, it can handle anonymous JavaScript object deserialization to expando objects.