1
votes

I am sending API requests with applications like Postman and Insomnia.

I can send a POST request with a Body that is form-data, so that it looks like

parameters:[{"fullname" : "John Smith", "username" : "jsmith", "email" : "jsmith@example.com", "password" : "s348nfdf"},{"fullname" : "Janine Smith", "username" : "jsmith1", "email" : "jsmith1@example.com", "password" : "ddfgret567"}]↵

And everything works, I can get the parameters from the request in the backend.

However, if I change the request to PUT, the body (parameters above) is not in the request ($_REQUEST). The request itself is successful with no error codes.

I have also tried x-www-form-urlencoded and raw JSON.

Why is the body (parameters) missing?

1
As per the docs, $_REQUEST only contains items from GET, POST and COOKIE. If yo uwant the body of PUT, use php://inputJonnix

1 Answers

1
votes

There is no body in $_REQUEST[]. Use:

$body = file_get_contents('php://input');

From the PHP manual entry on I/O streamsdocs:

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".