Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read HttpResponseMessage content as text

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;
}
like image 326
PythonIsGreat Avatar asked Apr 30 '15 18:04

PythonIsGreat


People also ask

How do I read the contents of HttpResponseMessage?

Open your immediate window and write response. Content. ReadAsStringAsync(). Result .

How do I set HttpResponseMessage content?

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.

How do I return HttpResponseMessage?

Depending on which of these is returned, Web API uses a different mechanism to create the HTTP response. Convert directly to an HTTP response message. Call ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message. Write the serialized return value into the response body; return 200 (OK).

What is HttpResponseMessage in C#?

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.


3 Answers

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().

like image 200
Bart van Nierop Avatar answered Oct 24 '22 04:10

Bart van Nierop


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.

like image 26
Timothy Shields Avatar answered Oct 24 '22 05:10

Timothy Shields


You can you ReadAsStringAsync() method

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

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

like image 9
Niraj Trivedi Avatar answered Oct 24 '22 06:10

Niraj Trivedi