Question

I'm sending POST request from command line (Ubuntu):

echo -n '{"prop":"value"}' | POST -c -U "application/json" http://site.com/test

Server script outputs its $_POST:

<?php 
  var_dump ($_POST);
?>

I see in output: Content-Length: 16, but in response of server I get

array(0){
}

Where have I mistaken?

Was it helpful?

Solution

$_POST contains the key-value pairs of the data submitted as normal form data. Because you sent JSON data, it doesn't get parsed the same.

You need to retrieve the request body. You can either use http_get_request_body() or treat the body as a file using fopen('php://input'). Once you have read the request body, you can use json_decode() to parse it.

$x = json_decode(http_get_request_body());

See:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top