• I am sending raw JSON POST data with cURL via PHP to a custom WP REST API endpoint i made, and the request body is received correctly when the JSON is not too long.
    However, the request body is empty when the JSON sent is bigger.

    This is how i get the body from the server (my custom WP REST endpoint):

    $dataReceived = file_get_contents("php://input");

    When i log $dataReceived on the server side, the expected content is in the log when not big, e.g. this:

    {"products":[],"allProductIds":["898","920","937","947","979","1007","1044","1064","1124","1146","1162","1178","1194","1215","1239","1291","1346","1395","1444","1629","1673","1695","1757","1868","1929","1992","2018","2050","2096","2112","2136","2178","2225","2280","2303","2318","2340","2394","2447","2459","2476","2486","2510","2520","2558","2591","2626","2655","2689","2727","2975","3175","3224","3283","3311","3333","3342","3353","3376","3389","3400","3419","3469","3506","3535","3610","3722","3765","3782","3802","4002","4020","4039","4060","4094","4130","4171","4213","4246","4281","4317","4348","4426","4473","4502","4545","4558","4563","4663","4688","4713","4733","4757","4875","4924","4984","5028","5057","5088","5102","5117","5145","5174","5197","5216","5236","5255","5275","5310","5336","5369","5422","5433","5464","5477","5505","5516","5541","5563","5587","5616","5627","5673","5703","5744","5773","5822","5839","5903","5999","6075","6196","6326","6483","6794","8360","8429","8599","8724","8798","8953","9150","9198","9247","9295","9347","9415","9463","9636","9991","10105","10219","10337","10451","10566","10681","10796","10912","11134","11244","11354","11465","11576","11687","11798","11874","11924","11973","12025","12074","12125","12175","12640","12699","12838","12844","12848","12852","12862","12866","12870","12874","12880","12884","12888","12894","12935","12943","12947","12952","12958","12962","12966","12970","12974","12979","12988","12992","12997","13001","13010","13012","13016","13020","13026","13030","13034","13039","13049","13059","13093","13128","13144","13148","13154","13158","13162","13172","13182","13217","13223","13368","2851","3828","4804"],"brandlyProductIds":["229082","226046","223246","216800","216750","215456","213613","213157","143436","143406","143362","143312","134066","134022","221020","242298","144021","143921","242348","333387","333424","333443","333517","333624","333681","334021","334043","334086","334135","334159","334184","334220","334261","334318","334351","334376","334398","334448","334505","334530","334552","334581","334606","334639","334672","334701","334734","334763","334792","334825","334962","335099","335325","335382","335439","335464","335482","335491","335500","335521","335532","335553","335575","335680","335770","335795","335877","335991","336032","336049","336066","336088","336276","336293","336310","336329","336370","336411","336452","336493","336530","336561","336598","336631","336705","336751","336782","336819","336850","336859","336977","337002","337033","337058","337083","337133","337207","337256","337366","337403","337428","337456","337473","337490","337515","337540","337565","337582","337599","337616","337637","337682","337710","337738","337791","337800","337831","337881","337906","337928","337953","337981","338006","338031","338056","338099","338135","338175","338201","338244","338269","338340","338422","187730","338815","338968","339121","339427","340235","340308","340461","340614","340687","340840","340987","341060","341133","341206","341279","341352","341425","341675","341981","342072","342254","342350","342441","342532","342714","342805","342896","343042","343115","343188","343261","343407","343480","343553","343626","343690","343754","343818","343882","343946","344010","225836","225770","344210","344214","344216","344218","303935","303610","303601","344223","344234","344236","344238","344241","204360","344254","344256","344262","344264","344268","344270","344272","344274","344277","344279","344281","344283","344285","344287","344288","344290","344292","344295","344299","344301","344303","344308","344313","344330","344333","344335","344337","344340","344342","344344","344349","344354","344371","150836","344434"]}

    But i get an empty $dataReceived for example when the sent JSON is the one in this pastebin https://pastebin.com/p5ZLrxig

    This is the code i have to send the request, with verbose log

    
        	protected static function _apiCall(string $url, array $additionalHeaders = [], string $method="GET", string $data=''){		
        
        		if($method=="POST"){
        			
        			$curlLog = __DIR__ . '/curl-log.txt';
        			$f = fopen($curlLog, 'w');
        			$ch = curl_init($url);
        			curl_setopt($ch, CURLOPT_VERBOSE, true);
        			curl_setopt($ch, CURLOPT_STDERR, $f);
        			curl_setopt($ch, CURLOPT_STDOUT, $f);
        			curl_setopt($ch, CURLOPT_POST, true);
        			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        			$hdr = [
        				'Content-Type: application/json',
        				'Content-Length: ' . strlen($data)
        			];
        			foreach($additionalHeaders as $h) $hdr[] = $h;
        			curl_setopt($ch, CURLOPT_HTTPHEADER, $hdr);
        			curl_setopt($ch, CURLOPT_POSTFIELDS, $data ); 
        			$info = curl_getinfo($ch);
        			$result = curl_exec($ch);
        			curl_close($ch);
        			fclose($f);
        			mail("[email protected]","$url curl info",print_r($info,true) . ", result:$result, curl log: " . file_get_contents("file://$curlLog") . "\r\n, data:$data");
        		} else { 
                  // ...
                }
        
        
        }

    This is the log i get in the email for the big JSON: https://pastebin.com/0pYHTk4h

    Now, can it be an issue with POST size limit, either in PHP or Apache settings?
    But i doubt so, since the data is just 33Kb

    Content-Length: 34581

    Also, if it was the case, shouldn’t we expect the server (Apache + PHP 7.4.x) to return an HTTP error code like HTTP 500?
    Instead, we have HTTP 100 and HTTP 200.

    Seems not related to JSON formatting too, again because we’d expect the server to return an error code.

    The called URL (from which i am logging php://input on server side) is a custom WordPress REST API endpoint (that i have created by calling register_rest_route inside a rest_api_init action handler).
    I am not currently aware of POST size limits or related issues with WP API, i have no idea if the issue can be with WP.

    Any hints are appreciated!

    • This topic was modified 2 years, 3 months ago by php-supadev.
    • This topic was modified 2 years, 3 months ago by php-supadev.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The server really only validates the data size and that the HTTP packet is properly formatted. Any other issues are up to the app to detect and respond appropriately. Getting a 200 response isn’t that surprising to me.

    There is a max. POST size limit set in php.ini, but it’s invariably much larger than 33k. Wouldn’t hurt though to verify with echo ini_get('post_max_size');.

    Try posting the same data to a stand-alone PHP file. If all the data there is contained in $__POST, then that eliminates any server issues and the problem is within the API or your particular endpoint. You could temporarily alter core API code to learn exactly where the problem occurs by using typical PHP debugging techniques.

    Before getting into debugging core code, try using the API to create a new post with your JSON as post content. If that works, the problem probably is not within the API itself. It may be time to take a closer look at your endpoint code.

    Thread Starter php-supadev

    (@lbdeveloper)

    UPDATE: @bcworkz ini_get('post_max_size') returned 332M, which i expected since i did put that in php.ini, and i still get an empty php://input also in the standalone PHP file!!
    $_POST is an empty array too

    While the smaller JSON gets to destination on standalone PHP too.

    The same happens if i send it as a form field instead of raw data:

    $header[] = 'Content-Type: application/x-www-form-urlencoded';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "json=" . urlencode($data) ); 
    • This reply was modified 2 years, 3 months ago by php-supadev.
    • This reply was modified 2 years, 3 months ago by php-supadev.
    • This reply was modified 2 years, 3 months ago by php-supadev.
    Thread Starter php-supadev

    (@lbdeveloper)

    This issue is very curious.

    The code that sends the data is in a WP plugin i am developing.

    I may avoid the issue with some dirty way, like putting the data in a temp and randomly named file on the web root of the WP site with my plugin.

    And i would just tell the server “hey, download this file with the data: https://wp-site.com/dfkndk.txt “, and i wait the reply from server.
    The server downloads and processes the file and replies “ok”, then i delete the file.

    But i guess my plugin would never be approved to the WP directory with a dirty practice like that.

    • This reply was modified 2 years, 3 months ago by php-supadev.
    Moderator bcworkz

    (@bcworkz)

    “332M” ?? Really? Or is that a typo? 32M is reasonable, 332M could run afoul of other server restrictions. Either way, no where close to your 33k issue.

    Why would you use file_get_contents() in API endpoint code to get passed data? Why not straight from $_POST? Seems like an unnecessary extra step to me. If you did go straight to $_POST in API endpoint code and it’s still empty, I have to suspect something odd in your server configuration.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘POST request body empty when received by custom WP REST endpoint’ is closed to new replies.