Yii2 rest api getBodyParams() can't get params from PUT request


How can I get "token" param from PUT request?


public function actionUpdate()
    $params = Yii::$app->getRequest()->getBodyParams();
    return $params;


curl -X PUT -H "Authorization: Bearer LL_nACyYVJFJyuHJxcOtiXu3OVNBJ_xo" -F "token=12345" "http://localhost/api/v1/devices/1"



I have tried this:

return $params['token'];

PHP Notice: Undefined index: token

And this

parse_str(file_get_contents("php://input"), $params);

Will return the same result

Show source
| rest   | php   | api   | yii2   | put   2017-01-04 22:01 1 Answers

Answers ( 1 )

  1. 2017-01-05 04:01

    i think the problem is related to the content type of your request. getting body params from put/post requires Content-type: application/x-www-form-urlencoded

    try using curl with -d instead of -F:

    curl -X PUT -H "Authorization: Bearer LL_nACyYVJFJyuHJxcOtiXu3OVNBJ_xo" -d "token=12345" "http://localhost/api/v1/devices/1"
◀ Go back