Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 Rest API PUT method call is not working

Tags:

rest

yii2

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 try from my POSTMAN App

For create I use POST http://localhost/myapp/api/v1/countries Works fine.But For Update I use PUT http://localhost/myapp/api/v1/countries/16 it returns 16 the record as JSON output not updating as expected.

What was wrong? Thanks!!

like image 462
Jackhad Avatar asked May 04 '16 10:05

Jackhad


1 Answers

In POSTMAN App, open the Request body tab and select x-www-form-urlencoded instead of form-data. That worked for me.

x-www-form-urlencoded selected

like image 70
Pablorotten Avatar answered Oct 02 '22 12:10

Pablorotten