0

I'm trying to do something that seems like it should be simple: to send a JSON request to a PHP script on my webserver and get the response.
I can request the site without issue, I can read the response without issue, but for some reason this refuses to send the contents of my JSON data.

string url = "https://www.exampleserver.com/examplescript.php";
HttpClient client = new HttpClient(new Xamarin.Android.Net.AndroidClientHandler());
client.BaseAddress = new Uri(url);
string jsonData = @"{""key1"" : ""data1"",""key2"" : ""data2"",""key3"" : ""data3""}";
HttpContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
string result = await response.Content.ReadAsStringAsync();
responsebox.Text = result;

Each time I run it the contents of responsebox.Text is replaced with the default contents of the page pointing out explicitly that there was no content in the $_POST data. (Even checked $_REQUEST to make sure it wasn't showing up as GET).
I know it's gotta be something simple, but I can't find it.

OL.
  • 201
  • 3
  • 13
  • Your sending code looks fine if you're trying to send JSON and not a form post. – ProgrammingLlama May 19 '18 at 08:01
  • See [this](https://stackoverflow.com/a/22662113/3181933) answer re the PHP side. – ProgrammingLlama May 19 '18 at 08:04
  • Please check `$HTTP_RAW_POST_DATA` in PHP and `$json = file_get_contents('php://input');` to check data. the first step is to see the data on server side. – RezaNoei May 19 '18 at 08:39
  • Thank you all! Switching to using `php://input` on the server side is what I was missing. The original version of the client was web-based so I thought all JSON data was going to be showing up through `$_POST`. Having it pull data from `php://input` corrected everything I was missing. – Quentin May 19 '18 at 16:21

0 Answers0