What's the difference between posting from a f

2019-09-03 09:31发布

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.

标签: php node.js http
2条回答
你好瞎i
2楼-- · 2019-09-03 10:17

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.

查看更多
趁早两清
3楼-- · 2019-09-03 10:21

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.

查看更多
登录 后发表回答