Calling the Yii2 Rest PUT API Method Doesn't Work

In my controller:

namespace app\api\modules\v1\controllers; use yii\rest\ActiveController; use yii\filters\VerbFilter; use yii\web\Response; class CountryController extends ActiveController { public $modelClass = 'app\models\Country'; public function behaviors() { return [ [ 'class' => 'yii\filters\ContentNegotiator', 'only' => ['index', 'view','create','update','search'], 'formats' => ['application/json' =>Response::FORMAT_JSON,], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'index'=>['get'], 'view'=>['get'], 'create'=>['post'], 'update'=>['PUT'], 'delete' => ['delete'], 'deleteall'=>['post'], 'search' => ['get'] ], ] ]; } }' 

I am trying from my postman application

For creation, I use POST http: // localhost / myapp / api / v1 / countries. It works fine. But for the update, I use PUT http: // localhost / myapp / api / v1 / country / 16 returns a 16 record as JSON output is not updated as expected.

What happened? Thanks!!

+7
source share
2 answers

In the POSTMAN application, open the Request Body tab and select x-www-form-urlencoded instead of form-data. It worked for me.

x-www-form-urlencoded selected

+5
source

Here is another option if you are comfortable using it. Instead of behaviors() you can add something like this, and it will serve the same purpose, and you will have no problems.

 public function actions() { $actions = parent::actions(); unset($actions['index']); unset($actions['create']); unset($actions['delete']); unset($actions['update']); unset($actions['view']); return $actions; } 
0
source

All Articles