0

On a php-server I capture all posted variables from $_POST, forexample

var_dump($_POST);

This works fine if I use a html-form to post to the server.

I try to post to the server using nodejs and requestify:

requestify.post('http://localhost/rest/1/comment/create', {hello: 'world'})
.then(function(response) {
    console.log(response.getBody());
})

This will not be captured by the php-server and var_dump($_POST) outputs an empty array.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Himmators
  • 14,278
  • 36
  • 132
  • 223

2 Answers2

0

When you submit an HTML form, the data will be encoded using the application/x-www-form-urlencoded format (unless you tell it to use text/plain or multipart/form-data).

PHP will automatically parse application/x-www-form-urlencoded and multipart/form-data into $_POST.

Restify will encode data as application/json, which PHP won't parse by default. This question details how to read JSON formatted POST requests.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You can use json_decode(file_get_contents("php://input"), true); snippet to read json request from PHP. php://input is PHP's STDIN where raw request is available. Details also available in docs.

Aleksey Ratnikov
  • 569
  • 3
  • 11