Slim icon indicating copy to clipboard operation
Slim copied to clipboard

getParsedBody() returns Null

Open JaySparkxz opened this issue 8 months ago • 1 comments

Forgive me, I am really new at this.

The raw JSON input is correctly received by PHP. The Content-Type: application/json header is correctly recognized by Slim. Yet, getParsedBody() still returns NULL and you needed a manual workaround.

PHP 8.4.6 (cli) (built: Apr 9 2025 09:45:15) (ZTS Visual C++ 2022 x64) Copyright (c) The PHP Group Zend Engine v4.4.6, Copyright (c) Zend Technologies

"name": "slim/slim", "version": "4.14.0",

and I am using a built-in server

<?php
// ... (existing code in auth.php) ...

$app->post('/add-user', function (Request $request, Response $response) use ($container) {
    // --- ADD THIS LINE FOR DEBUGGING ---
    $contentType = $request->getHeaderLine('Content-Type');
    error_log("Content-Type header received by Slim: " . $contentType);
    // --- END DEBUG LINE ---

    $data = $request->getParsedBody();
    
    error_log("Received data (var_export): " . var_export($data, true));
    error_log("Raw PHP input stream: " . file_get_contents('php://input'));

    try {
        // ... (rest of your /add-user code) ...

this code wasn't working. the data returned from getParsedBody() was Null

and this is from my error log

[Wed Jun 4 11:33:52 2025] Received data (var_export): NULL [Wed Jun 4 11:33:52 2025] Raw PHP input stream: { "name": "Another User", "email": "[email protected]", "password": "AnotherPassword123" } [Wed Jun 4 11:33:52 2025] [::1]:56303 Closing

JaySparkxz avatar Jun 04 '25 15:06 JaySparkxz

Hi @JaySparkxz I have two questions.

  1. Did you add the BodyParsingMiddleware?
$app->addBodyParsingMiddleware();
  1. Are you using the slim/http package?

odan avatar Jun 07 '25 08:06 odan

Closing this as done.

odan avatar Aug 17 '25 13:08 odan