Cannot access PUT request body params

34 views Asked by At

I am making this PUT request via Postman that has form-data body: PUT request

And trying to populate the model using this line:

$model->load(Yii::$app->request->getBodyParams(), '');

Trying to print body params

See, I tried to print worker_id, time_out params of the request on debug console, but getting null. And getBodyParams() is only returning an array of one ("image") element

Getting all params

1

There are 1 answers

0
Michal Hynčica On BEST ANSWER

For methods other than POST Yii2 doesn't use $_POST array to get body params. It uses mb_parse_str() on raw body instead. This approach probably have problem parsing multipart request body.

To make yii2 parse multipart body properly you have to add yii\web\MultipartFormDataParser to yii\web\Request::$parsers for multipart/form-data content type.

You can add it in your config like this:

return [
    // ...
    'components' => [
        'request' => [
            'parsers' => [
                'multipart/form-data' => 'yii\web\MultipartFormDataParser'
            ],
        ],
        // ...
    ],
    // ...
];