yii2: Yii::$app->getRequest()->getRawBody() returning empty

What steps will reproduce the problem?

Trying to get a JSON Post from an API the getRawBody() returns an empty string. Using var_dump(file_get_contents(‘php://input’)); in the frontend/web/index.php returns the string wanted.

Already tried to add this config/main.php

'request' => [
      'csrfParam' => '_csrf-frontend',
      'baseUrl' => '',
      'parsers' => [
        'application/json' => [
        'class' => \yii\web\JsonParser::class,
        'asArray' => true,
       ],
      ]
    ],

and this in the controller

/**
     * @inheritdoc
     */
    public function beforeAction($action)
    {
        $this->enableCsrfValidation = false;
        if (parent::beforeAction($action)) {
            return true;
        }
        return false;
    }

What is the expected result?

{
"id": "1bbc14c3-8ca8-492c-887d-1ca86400e4fa",
"key": "Example Key",
"type": "capture",
"status": "success",
"messages": [
"Your request was successfully created"
],
"date": "2022-01-01 10:20:30"
}

API Behing used: https://api.test.easypay.pt/docs#tag/Notification/paths/~1your-generic-notification-endpoint/post

What do you get instead?

Empty string.

Additional info

Q A
Yii version 2.0.10
PHP version 7.0
Operating system Ubuntu

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 24 (10 by maintainers)

Most upvoted comments

@bizley I used the same code I used in a Laravel Project with payments with the same API and Easypay sends a POST to the specific route with that json payload i get it in Laravel trough file_get_content('php://input) but i see in Yii you can use Request::getBodyParams() or getRawBody() but neither has any content.

I think I figured it out … I see a redirect because of the language. The webhook send info to /easypay/generic-info and it’s redirecting to pt/easypay/generic-info and maybe it’s losing the data there… i’m going to test this out on monday

@bizley it was a redirect. Fixed on the URL Manager config and now I can get the info using getRawBody() function. Sorry for the late response and thank you all for the help provided.