87

I'm using HttpResponseMessage class as a response from an AJAX call which is returning JSON data from a service. When I pause execution after the AJAX call comes back from the service, I see this class contains a Content property which is of type System.Net.Http.StreamContent.

If I inspect in the browser I see the network call being made successfully and the JSON data as the response. I'm just wondering why I cannot see the returned JSON text from within Visual Studio? I searched throughout this System.Net.Http.StreamContent object and see no data.

public async Task<HttpResponseMessage> Send(HttpRequestMessage request) { var response = await this.HttpClient.SendAsync(request); return response; } 
4
  • 1
    Perhaps you want to show us your code? Commented Apr 30, 2015 at 18:23
  • 1
    And what is it exactly that you want to do? Inspect the response variable? Commented Apr 30, 2015 at 18:27
  • yeah I was expecting to see JSON data that shows up as the response inside the chrome network panel to also be in that response object as it's content. Commented Apr 30, 2015 at 18:28
  • 2
    Open your immediate window and write response.Content.ReadAsStringAsync().Result. If your return type is JSON, you should see it there. Commented Apr 30, 2015 at 18:29

3 Answers 3

142

The textual representation of the response is hidden in the Content property of the HttpResponseMessage class. Specifically, you get the response like this:

response.Content.ReadAsStringAsync();

Like all modern Async methods, ReadAsStringAsync returns a Task. To get the result directly, use the Result property of the task:

response.Content.ReadAsStringAsync().Result;

Note that Result is blocking. You can also await ReadAsStringAsync().

Sign up to request clarification or add additional context in comments.

Comments

34

You can use ReadAsStringAsync on the Content.

var response = await client.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); 

Note that you usually should be using await - not .Result.

Comments

10

You can you ReadAsStringAsync() method

var result = await response.Content.ReadAsStringAsync(); 

We need to use await because we are using ReadAsStringAsync() which return task.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.