NotionCommotion Posted February 14, 2021 Share Posted February 14, 2021 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> Quote Link to comment https://forums.phpfreaks.com/topic/312149-retrieving-a-request-parameter-from-symfonys-request/ Share on other sites More sharing options...
kicken Posted February 14, 2021 Share Posted February 14, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312149-retrieving-a-request-parameter-from-symfonys-request/#findComment-1584461 Share on other sites More sharing options...
NotionCommotion Posted February 15, 2021 Author Share Posted February 15, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312149-retrieving-a-request-parameter-from-symfonys-request/#findComment-1584491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.