HttpClient.PostAsync not...posting?

So i’m doing something wrong here, and i’m not sure what it is.

(client = HttpClient)
var payload = "{\"CustomerId\": 5,\"CustomerName\": \"Pepsi\"}";
HttpContent c = new StringContent(payload, Encoding.UTF8, "application/json");
string response = await client.PostAsync("http://localhost/sandbox/postdump.php",c).Result.Content.ReadAsStringAsync();

My PHP page is:

<?php var_dump($_POST); ?>

But the result tells me that $_POST is an empty array.

What… have i done wrong?

I’m not 100% certain, but the fact that you are hiding the result by dialing into the response string immediately isn’t helping you debug it.

We use the SendAsync method instead of PostAsync, which this article also talks about (it highlights both ways).

At a minimum to help debug it, you should read the HttpResponseMessage result from PostAsync or SendAsync so you can see the status code, along with other details that might help you figure out what is happening.

Personally, I find it odd that PostAsync doesn’t seem to require you to set the Content property of the request message, like SendAsync does, as surely there should be a way to define the variable name of the content being passed via Post. Unless it is sending it as a buffer, in which case, $_POST will not work for reading it via PHP, you will have to use a different technique.

The request is completing, returning statuscode OK, it just… is not… sending the payload, or not sending it in a manner that the webserver is interpreting as appropriate POST data.

I did this to debug:

                var temp = await client.PostAsync("http://localhost/sandbox/postdump.php", c);
                var tres = temp.Content.ReadAsStringAsync();

and then ran the following in the Immediate Window after the second line:

temp.StatusCode
OK
temp.Headers
{Date: Tue, 02 Jun 2020 19:28:05 GMT
Server: Apache/2.4.38 (Win32) OpenSSL/1.1.1a PHP/7.3.2
X-Powered-By: PHP/7.3.2
}
    AcceptRanges: {}
    Age: null
    CacheControl: null
    Connection: {}
    ConnectionClose: null
    Date: {6/2/2020 7:28:05 PM +00:00}
    ETag: null
    Location: null
    Pragma: {}
    ProxyAuthenticate: {}
    RetryAfter: null
    Server: {Apache/2.4.38 (Win32) OpenSSL/1.1.1a PHP/7.3.2}
    Trailer: {}
    TransferEncoding: {}
    TransferEncodingChunked: null
    Upgrade: {}
    Vary: {}
    Via: {}
    Warning: {}
    WwwAuthenticate: {}
    Results View: Expanding the Results View will enumerate the IEnumerable
temp.Content
{System.Net.Http.HttpConnectionResponseContent}
    Headers: {Content-Length: 13
Content-Type: text/html; charset=UTF-8
}
temp.RequestMessage
{Method: POST, RequestUri: 'http://localhost/sandbox/postdump.php', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
  Content-Type: application/json; charset=utf-8
  Content-Length: 41
}}
    Content: {System.Net.Http.StringContent}
    Headers: {}
    Method: {POST}
    Properties: Count = 0
    RequestUri: {http://localhost/sandbox/postdump.php}
    Version: {1.1}
temp.RequestMessage.Content
{System.Net.Http.StringContent}
    Headers: {Content-Type: application/json; charset=utf-8
Content-Length: 41
}
temp.RequestMessage.Content.ReadAsStringAsync();
Id = 632, Status = RanToCompletion, Method = "{null}", Result = "{\"CustomerId\": 5,\"CustomerName\": \"Pepsi\"}"
    AsyncState: null
    CancellationPending: false
    CreationOptions: None
    Exception: null
    Id: 632
    Result: "{\"CustomerId\": 5,\"CustomerName\": \"Pepsi\"}"
    Status: RanToCompletion
tres
Id = 633, Status = RanToCompletion, Method = "{null}", Result = "array(0) {\n}\n"
    AsyncState: null
    CancellationPending: false
    CreationOptions: None
    Exception: null
    Id: 633
    Result: "array(0) {\n}\n"
    Status: RanToCompletion

The request went through. It’s just… not actually a POST request bearing data.

I tried mimicing the website suggested. Same result:

                var payload = "{\"CustomerId\": 5,\"CustomerName\": \"Pepsi\"}";
                HttpContent c = new StringContent(payload, Encoding.UTF8, "application/json");
                HttpRequestMessage r = new HttpRequestMessage
                {
                    Method = HttpMethod.Post,
                    RequestUri = new Uri("http://localhost/sandbox/postdump.php"),
                    Content = c
                };
                var sendtemp = await client.SendAsync(r);
                if (sendtemp.IsSuccessStatusCode)
                {
                    Console.WriteLine(sendtemp.Content.ReadAsStringAsync());
                }

(Breakpoint on the WriteLine.)
Breakpoint Immediate Window:

sendtemp.StatusCode
OK
sendtemp.Content
{System.Net.Http.HttpConnectionResponseContent}
    Headers: {Content-Length: 13
Content-Type: text/html; charset=UTF-8
}
sendtemp.Content.ReadAsStringAsync();
Id = 606, Status = RanToCompletion, Method = "{null}", Result = "array(0) {\n}\n"
    AsyncState: null
    CancellationPending: false
    CreationOptions: None
    Exception: null
    Id: 606
    Result: "array(0) {\n}\n"
    Status: RanToCompletion

Again, it’s returning. It’s just telling me that my request had no payload…

Instead of doing a var_dump on $_POST then, maybe set it up to return “OK!” or something to see if that comes back. My guess is the data is not going into the $_POST array and is likely a buffer being sent that you have to use ob_start for.

1 Like

But i needed it to go into the $_POST array.

Got there, by going the other way around, actually:
Instead of sending a StringContent, I needed to send a FormUrlEncodedContent, which then populated the $_POST array appropriately.

Yeah, that makes sense. StringContent was likely sending a buffer of data, not a key/value pair that you would see go into $_POST