Home > Back-end >  Problem parsing the body of a POST request
Problem parsing the body of a POST request

Time:07-03

So what I'm trying to do is: with a link similar to: http://localhost/API-REST/Endpoints/ajoutDeMessage.php?contenu='Hello'&idUser=4. For now, I'm only testing my request with Postman. So I'm putting this URL directly in Postman, and press send.

When I'm doing a request without a body, it works fine.

So, I need to send a string and an id through the URL, and with a function, I'm inserting these data in my database.

With the php://input, I expect to have the variables contenu and idUser in the body for a SQL request.

What I want to do next is a React app which will communicate with the API (but that's not related for now).

So here is my code:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    require_once('../Configuration/database.php');
    require_once('../Users/messages.php');

    $database = new Database();
    $db = $database->getConnection();

    $json = json_decode(file_get_contents("php://input"),true);
    var_dump($json);
    
    if(!empty($json->contenu) && !empty($json->idUser)){
        $contenu = strip_tags($json->contenu);
        $idUser = strip_tags($json->idUser);

        $message = new Messages($db);
        if ($message->addMessage($contenu,$idUser)){
            http_response_code(201);
            $response['message'] = 'GOOD ENTRY';
        }else{
            http_response_code(400);
            $response['message'] = 'BAD ENTRY';
        }
    }else{
        http_response_code(400);
        $response['message'] = 'INCOMPREHENSIBLE DATA';
    }

    echo json_encode($response);
} else {
    http_response_code(405);
    $response['error_code'] = 405;
    $response['message'] = 'BAD REQUEST TYPE';
    echo json_encode($response);
}

CodePudding user response:

There is no post body

i'm putting this url directly in postman, and press send.

I don't use postman myself, but doing this will generate a post request with no data it's the equivalent of:

curl -X POST http://example.com

That's not passing a post body at all. The intent is more like:

curl http://example.com
   -H 'Content-Type: application/json'
   -d '{"contenu":"Hello","idUser":4}'

This is why file_get_contents("php://input") doesn't return anything.

Note that html form data is available via $_POST - but only for urlencoded POST bodies (which I understand not to be the intent of the question).

Where is the data?

i'm putting this url directly in postman, and press send.

Returning to this quote, the only place for the data is in the url - that is not the normal way to pass data with a post request.

Url arguments are available via $_GET, with the url in the question this code:

<?php
var_dump($_GET);

will output:

array(2) {
  ["contenu"]=>
  string(7) "'Hello'"
  ["idUser"]=>
  string(1) "4"
}

A detail, but note the string includes the single quotes which are in the url (that are probably not desired).

What's the right way to do this?

With a request being made like this:

curl http://example.com
   -H 'Content-Type: application/json'
   -d '{"contenu":"Hello","idUser":4}'

That data can be accessed like so:

<?php
$body = file_get_contents('php://input');
$data = json_decode($body, true);
$jsonError = json_last_error();
if ($jsonError) {
  print_r(['input' => $body, 'error' => json_last_error_msg()]);
  exit;
}

echo $data['contenu']; // Hello
echo $data['idUser'];  // 4
...

This is very similar to the code in the question, the error appears to primarily be how the request is being made rather than the php logic.

  •  Tags:  
  • php
  • Related