Yii 2.0 Rest API in JSON (not in XML)

Hello everyone.

I am new in Yii2 and I am trying to create a simple RESTful API using the advanced app (frontend - backend)

In a very few steps I managed to get some response, but I can’t get it in JSON.

I have:

  • a frontend\models\Engineer model

  • a frontend\controllers\EngineerController


  namespace frontend\controllers;

  use yii\rest\ActiveController;

  class EngineerController extends ActiveController


      public $modelClass = 'frontend\models\Engineer';


  • and this is what I added to my configuration on frontend/config/main.php

  'urlManager' => [

    'enablePrettyUrl' => true,

    'enableStrictParsing' => true,

    'showScriptName' => false,

    'rules' => [

      ['class' => 'yii\rest\UrlRule', 'controller' => 'engineer'],



  'request' => [

    'parsers' => [

      'application/json' => 'yii\web\JsonParser',



but still I am getting XML response on /frontend/web/engineers.

Can someone help me to understand how can I get JSON response?

Thank you in advance.


Take a look at the guide

Hello skworden,

I did look at the guide and I did everything it says, but still I am missing something because the response is still in XML.

Can you help me understand the problem please?

Are you calling this endpoint with Accept Header set to application/json ?

The URL that I am using is ‘http://.../frontend/web/engineers’

and the result in the browser is:



What i mean - can you try to use POSTMAN to make a call, and set Accept header to type ‘application/json’. If this will return xml, then it looks like your config doesn’t work. Your code seems to be ok and in proper places and it’s probably some mistake - for example in index.php you’re referring to wrong config file or something like that.

Good luck with your search.

did u try to add a json formatter for the response?

Like(can´t post links):

Yii2 Cookbook: Outputting JSON and XML

Not sure if it helps, but my config is different than yours. In web config I have:

    'components' => [

        'request' => [

            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation

            'cookieValidationKey' => 'QMX0HrP82Cr0QJP55TqD1hjlFuh3xv_5',

            'parsers' => [

                'application/json' => 'yii\web\JsonParser',



That’s definitely outputting json for me when I use curl or postman.

same problem

what should i do? i got json in postman but xml in browser

Here is solution:

    public function behaviors()


        return [


                'class' => \yii\filters\ContentNegotiator::className(),

                'only' => ['index', 'view'],

                'formats' => [

                    'application/json' => \yii\web\Response::FORMAT_JSON,





I use it in controller.


[color="#006400"]/* Moved from "General Discussions" to "REST APIs" */[/color]

try adding ?_format=json to your url. it should work. by forcing json output.

you’re getting xml output because Accept header type is not set to ‘application/json’ as mentioned by @marcinu. and that is what postman as most frontend app usually do by default.


you helped me thank you but it just working in single controller is there any other solution to run get json format from all controller instead of putting this code in all controller