Postman向PHP发送POST请求失效问题咨询
Hey there, let's break down why your POST request isn't working in Postman while GET works perfectly. First, here's your code for reference:
<?php if(isset($_POST["test"])) echo $_POST["test"]; if(isset($_GET["test"])) echo $_GET["test"]; ?>
Common Causes & Fixes
Postman Request Format Mismatch
PHP's$_POSTsuperglobal only parses data sent in form-encoded formats (likeapplication/x-www-form-urlencodedormultipart/form-data). If you're sending raw JSON without setting the right headers,$_POSTwill be empty.- If you use
form-dataorx-www-form-urlencoded: Double-check that your parameter name is exactlytest(case-sensitive!) and that you've added it correctly in the request body. - If you send raw JSON: You'll need to read the request body directly since PHP doesn't auto-parse JSON into
$_POST. Modify your code like this:
And make sure to set<?php // Handle JSON POST data $rawPost = file_get_contents('php://input'); $postData = json_decode($rawPost, true); if(isset($postData["test"])) echo $postData["test"]; if(isset($_GET["test"])) echo $_GET["test"]; ?>Content-Type: application/jsonin Postman's Headers tab.
- If you use
PHP Configuration Issues
- Check your
php.inifile forpost_max_size: If your POST request body exceeds this value, PHP will discard the data entirely, leaving$_POSTempty. Increase it if needed (e.g.,post_max_size = 8M). - Ensure
enable_post_data_readingis set toOn: This directive controls whether PHP reads POST data into$_POST—if it's off,$_POSTwill always be empty.
- Check your
Debug with Error Reporting
Add error reporting to the top of your script to catch any hidden issues (like permission errors or configuration problems):<?php error_reporting(E_ALL); ini_set('display_errors', 1); if(isset($_POST["test"])) echo $_POST["test"]; if(isset($_GET["test"])) echo $_GET["test"]; ?>Also, replace your echo with
var_dump($_POST);temporarily to see exactly what data PHP is receiving—this will tell you if the issue is missing data or a parameter name typo.Case Sensitivity Check
Remember: PHP's superglobals are case-sensitive! If you accidentally sentTestinstead oftestin Postman,isset($_POST["test"])will return false. Double-check your parameter spelling and capitalization.
内容的提问来源于stack exchange,提问作者Luis Miguel Molina Betancourt

