Yii2: REST POST request parameters not arriving at action

I am doing REST API using Yii2.
I have included beautiful urlsvia /config/web.php

'urlManager' => [
        'enablePrettyUrl' => true,
        //'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => $routes,
]  

      

This is the content of the $ routes variable (basically just the content of a php script that has my rules separated from the web.php config file):

return [
        '<controller:[\w\-]+>/<id:\d+>' => '<controller>/view',
        '<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => '<controller>/<action>',
        '<controller:[\w\-]+>/<action:[\w\-]+>' => '<controller>/<action>',
        'api/<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => 'api/<controller>/<action>',
        'api/<controller:[\w\-]+>/<action:[\w\-]+>' => 'api/<controller>/<action>',
        'module/<module:[\w\-]+>/<controller:[\w\-]+>/<action:[\w\-]+>' => '<module>/<controller>/<action>',
        ['class' => 'yii\rest\UrlRule',
         'controller' => [
            'api/x_controller',
            'api/y_controller',
            'api/z_controller'
            ],
         'pluralize' => false,
        ]
];

      

And it works great, except that I cannot get any parameters that I am sending (I am targeting JSON, but also tried to send data application/x-www-form-urlencoded

and had the same results).
The current code that tries to get any parameters from the request:

public function actionDoSomething  
{
    $contentType = Yii::$app->request->getContentType();
    $raw = Yii::$app->request->getRawBody();
    $queryParams = Yii::$app->request->getQueryParams();
    $bodyParams = Yii::$app->request->getBodyParams();
    $token = Yii::$app->request->getBodyParam('access_token');
    $userID = Yii::$app->request->getBodyParam('user_id');
    $rawJson = file_get_contents("php://input");
}

      

Unfortunately, all of these variables are either empty or empty, although I am sending along with my POST request {"access_token":"XXXtoken","userId":"60"}

.

I am guessing this is something about my urlManager, but I cannot figure out what it is.

+3


source to share


2 answers


It looks like the problem is not with Yii2 at all.
After checking our GoDaddy configuration, we found that our entry was pointing to the wrong IP address.
After specifying the correct IP address, the problem was resolved.



To be honest, I would not expect the request to arrive at the server with this part incorrectly configured at all.

+2


source


In the past, I've used something like this in a staging location to parse multiple input types at once.



//Store the various HTTP methods to check against
$methods_to_check = array('POST', 'PUT');    

//Check if this request should be parsed
if(in_array(strtoupper(Yii::$app->request->getMethod()), $methods_to_check)){
    //Initialize the value to store data in
    $input = '';

    //Get reference to PHP input
    $file_handle = fopen('php://input', 'r');

    //Loop through PHP input until end of stream is reached
    while(!feof($file_handle)){
        $s = fread($file_handle, 64);

        $input .= $s;
    }

    //Close reference to PHP input
    fclose($file_handle);

    //Check if any data was passed, and merge the JSON-decoded version of it into the $_POST array
    if(!empty($input)) $_POST = array_merge($_POST, (array)json_decode($input));
}

      

0


source







All Articles