Yii::$app->request->post() not always getting data

I’m developing a REST controller and a web backend with the basic template.

I’m trying to post some values via (Firefox, RESTED Client), but I’m having a very weird performances. Sometimes, the post variables are catched, but in some others, they simply are not being catched, more if I check the $_POST variable and it is empty too.

It isn’t related, i guess, with the urlManager because as I said, the same URLs sometimes work, sometimes don’t work.

What do I do?:

On each function I need to check the access, I call inside $this->checkAccess('the-url'); and inside the checkAccess method:

    public function checkAccess($action, $model = null, $params = array())
    {
        if (Yii::$app->request->isPost) {
            $body = Yii::$app->request->post();
            Yii::debug(var_export($body, true)); // empty sometimes, the data some others.
            if ($action !== 'login' || $action !== "index") {
                if (!isset($body['auth_key'])) {
                    throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
                } elseif (($model = User::findIdentityByAccessToken($body['auth_key'])) === null) {
                    throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
                }
            }
        } else {
            if ($action !== 'login' || $action !== "index") {
                $body = Yii::$app->request->get();
                if (!Yii::$app->request->get("auth_key")) {
                    throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
                } elseif (($model = User::findIdentityByAccessToken(Yii::$app->request->get("auth_key"))) === null) {
                    throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
                }
            }
        }
    }

If I use rawBody, the data is printed as string, just like the URL query string.

I have to try a lot of times before the post parameters are finally catched.

Can you show us how you perform the POST request on the client side ?

I only can to show a part:

var request = http.MultipartRequest('POST', Uri.parse(url));
request.fields.addAll(data); // data: Map<String, String>
request.fields.add(await http.MultipartFile.fromPath("picture", fileName);

http.StreamedResponse response = await request.send();
final res = await http.Response.fromStream(response);

BTW, with Postman, now I’m getting the values, but again, from the mobile app I’m unable to get them.

If it works with postman, make sure to compare the exact request headers sent from Postman, and the one from your flutter request.

For example, make sure your Content-type request header is set to application/json, so that Yii knows to decode it automatically when using Yii::$app->request->post() :
yii2/Request.php at 7ebaaf021680bc40606931a808541cf298f43938 · yiisoft/yii2 · GitHub

If it works with postman, make sure to compare the exact request headers sent from Postman, and the one from your flutter request.

For example, make sure your Content-type request header is set to application/json, so that Yii knows to decode it automatically when using Yii::$app->request->post() :
https://github.com/yiisoft/yii2/blob/7ebaaf021680bc40606931a808541cf298f43938/framework/web/Request.php#L585