Jump to content

Retrieving a request parameter from Symfony's Request


Recommended Posts

Doing something wrong, but don't see it.  How should one retrieve a POST parameter?  My $request->toArray()['html'] works, but I am sure it is not the "right way".

<?php
namespace App\DataPersister;
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class ArchivePersister implements DataPersisterInterface
{
    public function __construct(RequestStack $requestStack)
    {
        $request = $requestStack->getCurrentRequest();
        syslog(LOG_ERR, '$request->getMethod(): '.$request->getMethod());
        syslog(LOG_ERR, '$request->getContent(): '.$request->getContent());
        syslog(LOG_ERR, '$request->request->get(html): '.$request->request->get('html'));
        syslog(LOG_ERR, '$request->query->get(html): '.$request->query->get('html'));
        syslog(LOG_ERR, '$request->get(html): '.$request->get('html'));
        syslog(LOG_ERR, '$request->toArray(): '.json_encode($request->toArray()));
        syslog(LOG_ERR, '$request->toArray()[html]: '.$request->toArray()['html']);
    }
}

output

$request->getMethod(): POST
$request->getContent(): {"project":"/projects/1","description":"","html":"<p>{{ project_name }}</p>"}
$request->request->get(html):
$request->query->get(html):
$request->get(html):
$request->toArray(): {"project":"\/projects\/1","description":"","html":"<p>{{ project_name }}<\/p>"}
$request->toArray()[html]: <p>{{ project_name }}</p>
Link to post
Share on other sites

If your post body is json then it seems you need to either use getContent to grab the json and parse it manually or use toArray to grab then parsed array.

$request->request->get() is how you would get normal post parameters but it seems that it doesn't process json documents and make the keys available via it.

 

Link to post
Share on other sites
On 2/14/2021 at 8:17 AM, kicken said:

If your post body is json then it seems you need to either use getContent to grab the json and parse it manually or use toArray to grab then parsed array.

$request->request->get() is how you would get normal post parameters but it seems that it doesn't process json documents and make the keys available via it.

 

Thanks kicken,  I am surprised and was sure it was user error on my part.  I think I am sending the correct content type header, but maybe symfony's request class isn't that sophisticated.  Still hard to believe.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.