1

I have a custom controller that expects POST data. I had it working fine in 8.1.0 retrieving the data using \Drupal::request()->request->all() and \Drupal::request()->request->get($myvar).

Now, without any changes in my custom module, \Drupal::request()->request->all() is returning an empty array, and \Drupal::request()->request->get($myvar) is returning NULL.

Between the time it was working and when the issue arose, my hosting service Pantheon updated their Drupal 8 version from 8.1.0 to 8.1.1 and then 8.1.2. I suspect something may have changed then that affected this.

How can I fix this?

EDIT: Thanks for the comments, still not quite working...

Route YAML:

mymodule.foobar:
  path: '/foobar'
  options:
    _auth: [ 'basic_auth' ]
  defaults:
    _controller: '\Drupal\mymodule\Controller\FooBarController::foobar'
    _title: 'foobar'
  requirements:
    _user_is_logged_in: 'TRUE'
    _permission: 'administer site configuration'

Controller:

<?php
namespace Drupal\mymodule\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
/**
 * Class FooBarController.
 *
 * @package Drupal\mymodule\Controller
 */
class FooBarController extends ControllerBase {
  /**
   * Provides content for the /mymodule/foobar path.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request
   */
  public function foobar(Request $request) {
    $content = $request->getContent();
    $data = array();
    $data['a'] = $request->get('foo');
    $data['b'] = $request->request->get('foo');
    $data['c'] = \Drupal::requestStack()->getCurrentRequest()->request->get('foo');
    $data['e'] = $request->request->all();
    $data['f'] = $content;

    $request = \Drupal::requestStack()->getCurrentRequest();
    $content = $request->getContent();
    $data['g'] = $request->get('foo');
    $data['h'] = $request->request->get('foo');
    $data['j'] = $request->request->all();
    $data['k'] = $content;

    return new JsonResponse((object) $data);
  }
}

Postman Request: enter image description here

Note: This was working before with this exact code, correctly using basic HTTP authentication. There have been no changes other than these variations to my controller, and core updates.

Any more ideas?

6
  • Can you show us the routing yml as well, and also the url with query you type in the browser? (eg //localhost/foo?foo="hello")
    – ssibal
    Commented Jun 9, 2016 at 10:09
  • @ssibal good point. Added.
    – baisong
    Commented Jun 9, 2016 at 16:56
  • Can you var_dump please the $_REQUEST variable?
    – ssibal
    Commented Jun 10, 2016 at 8:38
  • Aha, both $_REQUEST and $GLOBALS['_REQUEST'] are empty arrays. I've openned a ticket with my host to see if there is perhaps some PHP server configuration issue here...
    – baisong
    Commented Jun 10, 2016 at 18:45
  • I'm having the same issue except I only see data when using form-urlencoded which corrupts the posted data. The request parameters are always empty when I use json.
    – joekers
    Commented Jun 7, 2019 at 10:33

2 Answers 2

0

I had the same issue and the request parameters were always empty. I think it could be due to using it in a controller rather than a REST endpoint but I'm not sure.

The following line of code worked for me:

$request->getContent();
-1

Try this:

$request = \Drupal::requestStack()->getCurrentRequest()->query->all();

Sidenote: Using a Controller you shouldn't use "\Drupal::..." static request call. Instead, you always have in your controller's enter point function a "Request $request" object as a last argument.

1
  • 2
    "query" is for GET parameters, the question asked about POST parameters.
    – anonymous
    Commented Mar 28, 2022 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.