SkyeBoniwell
SkyeBoniwell

Reputation: 7122

Get string returned from a PostAsync event

I'm using HttpClient like this in my console app:

        using (var http = new HttpClient(handler))
        {
            http.BaseAddress = new Uri("http://127.0.0.1:34323/");

            var response = await http.PostAsync("/api/generate", new StringContent(
            JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"));

            Console.WriteLine(response.Content.ToString());   
        }

In debug mode, I can see that the controller is returning a string of JSON.

However, I only get this written to the console:

System.Net.Http.StreamContent

How can I get it to write the actual JSON that's being returned?

Thanks!

Upvotes: 0

Views: 174

Answers (1)

Krunal Mevada
Krunal Mevada

Reputation: 1655

Try below line:

Console.WriteLine(response.Content.ReadAsStringAsync().Result.ToString());

Upvotes: 1

Related Questions