Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php://input is empty on POST request

Problems with multipart/form-data forced me to parse POST request's parameters manually as I already doing for PUT requests. For that purpose I used this code:

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

But I figured that php://input is always empty for POSTs, at least, for php-fpm SAPI.

Here is some pics from debugger. POST request:

empty $rawData

PUT with same params:

filled $rawData

Is there a way to get raw POST request body? Thanks in advance.

like image 340
Vitaly Chirkov Avatar asked Aug 17 '26 08:08

Vitaly Chirkov


1 Answers

Before PHP 5.4 $HTTP_RAW_POST_DATA is not available with enctype="multipart/form-data" (with the exception of some SAPI implementations), explanations here:

  • http://www.php.net/manual/en/wrappers.php.php
  • http://www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data

I suggest you give a look to a couple of answers to existing questions:

  • php http request content raw data enctype=multipart/form-data
  • userland multipart/form-data handler

From PHP 5.4+ you can use the php.ini directive enable_post_data_reading to disable PHP consuming the raw data (hence process it), be aware that $_POST and $_FILES won't be populated though (refer to Vitaly Chirkov answer).

like image 68
David Riccitelli Avatar answered Aug 18 '26 23:08

David Riccitelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!