yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

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

Closed marcelosantos89 closed 2 years ago

marcelosantos89 commented 2 years ago

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
WinterSilence commented 2 years ago

api.test.easypay.pt it's your site?

marcelosantos89 commented 2 years ago

It's not my site it's the result i was expecting i'll update the main post with the result expected

WinterSilence commented 2 years ago

@marcelosantos89 it's server request i.e. request to your server. use yiisoft/yii2-httpclient to send external requests

marcelosantos89 commented 2 years ago

it's a POST that API makes to my Yii2 application after I ask for some payment information (returning if the payment was successful or not) but i can't get the JSON payload in Yii2 using what was expected to have the info

bizley commented 2 years ago

Please update your Yii to the latest version first.

marcelosantos89 commented 2 years ago

@bizley can't really do that. it's so far behind right now it breaks everything. I can try upgrading after this "emergency" is complete but cant do it right now

WinterSilence commented 2 years ago

@marcelosantos89 api redirect to your site or use webhook? i'm don't see any callbacks in api description

marcelosantos89 commented 2 years ago

@WinterSilence it's a webhook. It sends the data of payment after the person has paid for an order.

WinterSilence commented 2 years ago

@marcelosantos89 Request::getBodyParams() too empty?

marcelosantos89 commented 2 years ago

@WinterSilence yes it's also empty.

marcelosantos89 commented 2 years ago

@bizley i tried to update my yii2 with this: composer require "yiisoft/yii2:~2.0.45" --update-with-all-dependencies

got this:

  Problem 1
    - Root composer.json requires yiisoft/yii2 ~2.0.45 -> satisfiable by yiisoft/yii2[2.0.45].
    - yiisoft/yii2 2.0.45 requires bower-asset/jquery 3.6.*@stable | 3.5.*@stable | 3.4.*@stable | 3.3.*@stable | 3.2.*@stable | 3.1.*@stable | 2.2.*@stable | 2.1.*@stable | 1.11.*@stable | 1.12.*@stable -> found bower-asset/jquery[2.2.4] in the lock file but not in remote repositories, make sure you avoid updating this package to keep the one from the lock file.
  Problem 2
    - facebook/graph-sdk is locked to version 5.5.0 and an update of this package was not requested.
    - facebook/graph-sdk 5.5.0 requires php ^5.4|^7.0 -> your php version (8.0.11) does not satisfy that requirement.
  Problem 3
    - yiisoft/yii2 2.0.45 requires bower-asset/jquery 3.6.*@stable | 3.5.*@stable | 3.4.*@stable | 3.3.*@stable | 3.2.*@stable | 3.1.*@stable | 2.2.*@stable | 2.1.*@stable | 1.11.*@stable | 1.12.*@stable -> found bower-asset/jquery[2.2.4] in the lock file but not in remote repositories, make sure you avoid updating this package to keep the one from the lock file.
    - yiisoft/yii2-faker 2.0.3 requires yiisoft/yii2 * -> satisfiable by yiisoft/yii2[2.0.45].
    - yiisoft/yii2-faker is locked to version 2.0.3 and an update of this package was not requested.
WinterSilence commented 2 years ago

composer update yiisoft/yii2 -W -o

marcelosantos89 commented 2 years ago

@WinterSilence it doesn't work now :D

yii\base\InvalidConfigException: Unsupported configuration type: object in g\application\vendor\yiisoft\yii2\BaseYii.php:353
Stack trace:
#0 \application\vendor\yiisoft\yii2-debug\src\Module.php(268): yii\BaseYii::createObject(Object(yii\debug\LogTarget), Array)
#1 \application\vendor\yiisoft\yii2\base\Application.php(325): yii\debug\Module->bootstrap(Object(yii\web\Application))
#2 \application\vendor\yiisoft\yii2\web\Application.php(69): yii\base\Application->bootstrap()
#3 \application\vendor\yiisoft\yii2\base\Application.php(271): yii\web\Application->bootstrap()
#4 \application\vendor\yiisoft\yii2\base\BaseObject.php(109): yii\base\Application->init()
#5 \application\vendor\yiisoft\yii2\base\Application.php(204): yii\base\BaseObject->__construct(Array)
#6 \application\frontend\web\index.php(18): yii\base\Application->__construct(Array)
#7 {main}
WinterSilence commented 2 years ago

@marcelosantos89 ups.. sorry, composer update -W -o

marcelosantos89 commented 2 years ago

@WinterSilence i already did that one to 👍 the error continues Managed to fix it .. it was Yii debug bar

marcelosantos89 commented 2 years ago

Please update your Yii to the latest version first.

updated the framework so it just works in this function still returning empty

bizley commented 2 years ago

The expected result is wrong if I'm not mistaken, you should see the response example from easypay, not a payload. ~I'm sorry if the next question sounds trivial but are you sure they are able to send anything to your app? You can see the payload in php://input because this is what you are sending to them, the other way around is different.~

bizley commented 2 years ago

Sorry, that was weird what I said :P The question is - is the response status 200? Maybe you don't get anything in response because of the wrong payload data or something.

marcelosantos89 commented 2 years ago

@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 commented 2 years ago

@marcelosantos89 any news?

marcelosantos89 commented 2 years ago

@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.

chegue2009 commented 1 year ago

Hello how to use data come from getRawBody() ? Because i just see after send request the data like a string "{\r\n \"data\": [\r\n 618,\r\n 606,\r\n 603,\r\n 597,\r\n 593,\r\n 586,\r\n 585,\r\n 581,\r\n 574,\r\n 554,\r\n 537,\r\n 534,\r\n 529,\r\n 528,\r\n 515,\r\n 511,\r\n 509,\r\n 497,\r\n 479,\r\n 482,\r\n 475,\r\n 473,\r\n 458,\r\n 447,\r\n ]\r\n}\r\n "

this is the Json i send : { "data": [ 618, 606, 603, 597, 593, 586, 585, 581, 574, 554, 537, 534, 529, 528, 515, 511, 509, 497, 479, 482, 475, 473, 458, 447, 446, 432, 428, 424, 346, 398 ] }

this option no work just send empty Yii::$app->request->post('data') but getRawBody() send string can i get a solution please. I want to get datas come from getRawBody()

richnwa commented 1 year ago

@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.

Can you explain in more detail what you mean?

marcelosantos89 commented 1 year ago

@richnwa i have a package to handle languages and had to create a exception for that controller and method so it didn't try to put a language on the url. domain.com/controller/method was redirecting to domain.com/en/controller/method and i lost the getRawBody() content.