0

This is probably an annoying rookie-mistake, but I can't find a solution by googling.

I tried sending this post from Visual basics (and from a Javascript-script as well, neither worked)

POST [Deleted the url here]/posttest.php HTTP/1.1
content-type: application/json

{
    "name": "sample",
    "time": "Wed, 21 Oct 2015 18:27:50 GMT"
}

Also tried this with just "testing" as a body:

var xmlhttp = new XMLHttpRequest();

  xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
      callback(xmlhttp.response);
    }
  };

  xmlhttp.open('POST', 'posttest.php', true);
  xmlhttp.send("testing");

My Php-code doesn't echo any body at all, trying all kinds of concepts, all echos are completely empty of text:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
      foreach ($_POST as $key => $value) {
        echo $key;
        echo $value;
    }
    
  echo "raw: " . $HTTP_RAW_POST_DATA;
  echo isset($_POST['name']);
  echo "post" . implode(", ", $_POST);
  $name = $_POST['name'];
  if (empty($name)) {
    echo "Name is empty";
  } else {
    echo $name;
  }
}
?>

Does anybody find the fault or have any idea how to get the body showing in the post?

ruja
  • 3
  • 2
  • Yes, thank you! Why doesn't $_POST work with either the REST post from Visual Studio or from javascript, anyone knows? – ruja Jun 26 '20 at 13:41
  • It does not work, because your request is `content-type: application/json`. https://www.php.net/manual/en/reserved.variables.post.php explains, that only `application/x-www-form-urlencoded` or `multipart/form-data` requests are automatically processed by PHP to be made available in $_POST. – CBroe Jun 26 '20 at 13:45

0 Answers0