0

I'm performing a POST call that returns some JSON. I have a function that works and uses HttpWebRequest, HttpWebResponse, and StreamReader to fire off the POST call and print back the returned JSON.

I'm now trying to create a new function that does the exact same thing but using an async call using HttpClient and HttpResponseMessage. Everything seems to work except when printing the json results. Instead of getting a deserialized json object with object fields and such, I get this:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Transfer-Encoding: chunked
  Date: Fri, 16 Sep 2016 21:11:29 GMT
  Server: Kestrel
  Content-Type: text/plain; charset=utf-8
}

This seems to be information about the response, but not the actual JSON I was expecting. I'm completely new to web development and HttpClient - does anyone know what I'm doing wrong? Here's my HttpClient POST method:

public async static Task<HttpResponseMessage> POSTDataHttpContent(string jsonString, string webAddress)
{
    using (HttpClient client = new HttpClient())
    {
        StringContent stringContent = new StringContent(jsonString);
        HttpResponseMessage response = await client.PostAsync(
            webAddress,
            stringContent);

        Console.WriteLine("response is: " + response);

        return response;
    }
}
Roka545
  • 3,404
  • 20
  • 62
  • 106
  • is stringContent populated with what you think it should be? – Ju66ernaut Sep 16 '16 at 21:30
  • 3
    From [this answer](http://stackoverflow.com/a/29489709/879997), it looks like the body is stored in the `response.Content` property, and you can get the text as a string by calling `await response.Content.ReadAsStringAsync();`. – Andrew Sep 16 '16 at 21:32
  • 6
    Possible duplicate of [Getting content/message from HttpResponseMessage](http://stackoverflow.com/questions/15936180/getting-content-message-from-httpresponsemessage) – Andrew Sep 16 '16 at 21:33
  • 1
    @hmm, this code snippet looks oh so familiar :) And Andrew is right on both counts...this is a duplicate and that is all you need to get the content. However, if you do, you need to change your method signature to `Task`. – David L Sep 16 '16 at 21:34

0 Answers0