0

I writing app for UWP, which requests server with POST method in server I am not getting data in $_POST This is my code sample

 public async void POSTreq()
   {
      Uri requestUri = new Uri("http://www.test.com");
      dynamic dynamicJson = new ExpandoObject();
      dynamicJson.username = "example@gmail.com".ToString();
      dynamicJson.password = "123456789";
      dynamicJson.action = "REGISTERUSER";
      string json = "";
      json = Newtonsoft.Json.JsonConvert.SerializeObject(dynamicJson);
      var objClint = new System.Net.Http.HttpClient();
      System.Net.Http.HttpResponseMessage respon = await objClint.PostAsync(requestUri, new StringContent(json,System.Text.Encoding.UTF8,"application/json"));   
   }

In server side I am getting empty POST

DEBUG: == post()     [Array\n(\n)\n]
DEBUG: == get()     [Array\n(\n)\n]
DEBUG: == server()     [Array\n(\n    [CONTENT_LENGTH] => 55\n    [CONTENT_TYPE] => application/json; charset=utf-8\n    [HTTP_HOST] => www.taskera.com\n    [HTTP_CONNECTION] => Keep-Alive\n    [PATH] => /sbin:/usr/sbin:/bin:/usr/bin\n    [SERVER_SIGNATURE] => <address>Apache/2.2.15 (CentOS) Server at www.taskera.com Port 80</address>\n\n    [SERVER_SOFTWARE] => Apache/2.2.15 (CentOS)\n    [SERVER_NAME] => www.taskera.com\n    [SERVER_ADDR] => 192.168.1.187\n    [SERVER_PORT] => 80\n    [REMOTE_ADDR] => 192.168.1.34\n    [DOCUMENT_ROOT] => /var/www/html\n    [SERVER_ADMIN] => root@localhost\n    [SCRIPT_FILENAME] => /var/www/html/index.php\n    [REMOTE_PORT] => 50574\n    [GATEWAY_INTERFACE] => CGI/1.1\n    [SERVER_PROTOCOL] => HTTP/1.1\n    [REQUEST_METHOD] => POST\n    [QUERY_STRING] => \n    [REQUEST_URI] => /\n    [SCRIPT_NAME] => /index.php\n    [PHP_SELF] => /index.php\n    [REQUEST_TIME] => 1487390506\n)\n]
Shashi kumar S
  • 873
  • 3
  • 10
  • 21

1 Answers1

1

Your client code is OK so the issue is quite likely on your server's side. This is how RequestBin sees your request:

FORM/POST PARAMETERS

None HEADERS

Via: 1.1 vegur Cf-Ray: 3330562272bd5b3f-HEL Cf-Visitor: {"scheme":"http"} Content-Length: 79 Cf-Ipcountry: FI Connection: close Connect-Time: 0 X-Request-Id: e51d5210-7b13-497d-bb78-c7cbe54e2d41 Content-Type: application/json; charset=utf-8 Total-Route-Time: 0 Host: requestb.in Cf-Connecting-Ip: 91.155.129.79 Accept-Encoding: gzip RAW BODY

{"username":"example@gmail.com","password":"123456789","action":"REGISTERUSER"}

From here you can find a mention that $_POST only supports the following content types:

  • application/x-www-form-urlencoded (standard content type for simple form-posts)
  • multipart/form-data-encoded (mostly used for file uploads)

You should be able to read the POST body using file_get_contents('php://input'). More info about the php://input is available from PHP's manual.

Community
  • 1
  • 1
Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63