H
H
hollanditkzn2018-03-29 16:54:42
Yii
hollanditkzn, 2018-03-29 16:54:42

How to make json format in api?

I have such a question in postman, I get a json response, but let's say when I open this page in the browser I get the xml format, should it be so or not?
In config/main.php

'components' => [
       ....
        'response' => [
          'formatters' => [
              \yii\web\Response::FORMAT_JSON => [
                  'class' => 'yii\web\JsonResponseFormatter',
              ]
          ]
        ],

I registered this, but I get xml in the browser, I wanted to understand maybe, and it should be if postman gives json format

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Khomenko, 2018-03-29
@hollanditkzn

When the browser makes a request to the server, it indicates which content types it prefers in the form of a header
. In the framework, REST controllers are configured for two data types - JSON and XML
. the browser has indicated it as the preferred one. If you want to leave only JSON, then reconfigure the controllers. And then even in the browser there will be JSON.
Read full description: https://github.com/yiisoft/yii2/blob/master/docs/g...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question