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

Question

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

Controller:

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

Request:

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

Response:

{"success":true,"data":{"--------------------------580af3364bd175a7\r\nContent-Disposition:_form-data;_name":"\"token\"\r\n\r\n12345\r\n--------------------------580af3364bd175a7--\r\n"}}r

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