Search code examples
restsymfonypostman

Symfony post request body parameters?


I'm sending POST request by postman with header application/json and the body:

{
    "name": "user"
}

And when I try to get this parameter from the request object $request->request->get('name') I got null. But when I use $request->getContent() I receive raw string.

Looks like my request is not parsed correctly. What is wrong with the request?

Update:

Turned out that docs not clear about that and I need to manually convert body to json. Don't really understand why not to do it in framework by default.


Solution

  • That is the expected behavior. You are sendind a JSON string inside the body of the request.

    In this case, you need json_decode to convert the JSON string into an array or object, in order to access the data.

    $parameters = json_decode($request->getContent(), true);
    echo $parameters['name']; // will print 'user'