2

Debugger shows, that there is POST data in request, but I cannot get it by $request->get('foo');

And $request->request->all(); gives array [0].

My Ajax POST request (by AngularJS):

...
    $http.post('http://admin/about-company/setJSONObj',
        {foo: 'bar'}
    );

My controller with debugging info in comments (Symfony 3.2.9):

use Symfony\Component\HttpFoundation\Request;
    ...
    public function updateAction(Request $request)
        {
             $foo = $request->get('foo'); // null
             $requestType = $request->getContentType(); // json
             $content = $request->getContent(); // {"foo":"bar"}

I used these approach on Symfony 2.7 project, and it worked fine, but I'm not sure, what's happening in these case?

Also, maybe there is any Symfony framework config variable that tells not to parse POST data, or hides it during caching the request?

Parks
  • 71
  • 1
  • 7
  • 1
    did you try $request->request->get('foo'); – Jurij Jazdanov Jun 02 '17 at 12:12
  • I don't know about pre 2.8 (started late) but see [symfony-request-object](http://symfony.com/doc/current/introduction/http_fundamentals.html#symfony-request-object) – Jenne Jun 02 '17 at 12:13
  • Thanks guys, seems I tried all such variants and not worked. I think soon I'll parce json from $content to roughly fix this ))) – Parks Jun 02 '17 at 12:19
  • Have you passed a proper _Content-Type_ header? – eRIZ Jun 02 '17 at 12:21
  • 1
    @eRIZ, nope, I think AngularJS cares about headers. By the way it sends Content-Type:application/json;charset=UTF-8 – Parks Jun 02 '17 at 12:28
  • if you are using Angular, please check this [answer](https://stackoverflow.com/questions/12190166/angularjs-any-way-for-http-post-to-send-request-parameters-instead-of-json). – Fnayou Jun 02 '17 at 15:25

3 Answers3

1

For POST request is:

$request->request->get('foo');
Ilario Pierbattista
  • 3,175
  • 2
  • 31
  • 41
0

Try using FormData for your client-side ajax's call

For example : try something like

var formData = new FormData();
formData.append('foo', 'bar')

  $http.post('http://url',
       formData
  );

Ok, I wasn't paying attention that you used json so, You wont get the content of $foo in request but you need to json_decode the $content

So by keeping the same way you sent data :

$http.post('http://admin/about-company/setJSONObj',
    {foo: 'bar'}
);

You just have to call

use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    ...
    /**
    * @Route("/setJSONObj", name="admin_pages_set_ajax_obj")
    * @Method("POST")
    */
    public function updateAction(Request $request)
        {
             $foo = $request->get('foo'); // null
             $requestType = $request->getContentType(); // json
             $content = $request->getContent(); // {"foo":"bar"}
             $data = json_decode($content, true);
             dump($data['foo']); // "foo"
             //And you can know replace the data of the request. So
             $request->request->replace($data);

My guess is because symfony need have the 'Content-Type' 'application/x-www-form-urlencoded' but Angular by default have a application/json (so you can override the headers on the $http.post call.

henrily
  • 236
  • 1
  • 5
  • The same, except $content = `------WebKitFormBoundaryHLFYNZeven585hAi Content-Disposition: form-data; name="foo" bar ------WebKitFormBoundaryHLFYNZeven585hAi--` – Parks Jun 02 '17 at 12:39
  • thanks for variant, but things was already like you suggesting: 'json application-type' header sets AngularJS for request, and Symfony don't handle it – Parks Jun 02 '17 at 13:26
  • json_decode - yes, it should work, but I'll use if not find better solution ) – Parks Jun 02 '17 at 13:30
  • have you tried to change the header to `'application/x-www-form-urlencoded'` and use `new FormData()` ? This one should work – henrily Jun 02 '17 at 13:37
  • yes, it changed $type = `form`, `...request->all()` gives same result as `$content` in 1st comment, but `...->get('foo')` gives `null`... – Parks Jun 02 '17 at 16:19
0

It turned out, my mistake was to expect POST variables in $request->request->all(); while posting JSON: see explanation https://www.toptal.com/....

For creating REST api with Symfony it's normal to use FOSRestBundle: see body listener. So, it solves my problem in elegant way.

Also, @henrily suggested a workaroud somebody can use, but it's just a workaround.

Parks
  • 71
  • 1
  • 7