trance  
          
              
                March 25, 2016, 11:49am
               
              1 
           
         
        
          I have set up my restfull UserController like in the guide : http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html 
When I want to display one user, I visit this url: localhost/api/users/1 , and I get xml response. How can I get JSON by default for all my controllers ?
         
        
           
         
            
       
      
        
        
          In config/main.php insert this component, inside ‘component’ node:
'components' => [
    ...
    ...
    
        'response' => [
        
            'format' => yii\web\Response::FORMAT_JSON,
            'charset' => 'UTF-8',
        ], 
    ...
    ...
]
 
        
           
         
            
       
      
        
          
          
            trance  
          
              
                March 25, 2016, 11:55am
               
              3 
           
         
        
          
In config/main.php insert this component, inside ‘component’ node:
'components' => [
    ...
    ...
    
        'response' => [
        
            'format' => yii\web\Response::FORMAT_JSON,
            'charset' => 'UTF-8',
        ], 
    ...
    ...
]
 
 
Thanks, but I still get xml. BTW I am using basic template.
         
        
           
         
            
       
      
        
        
          What kind of input are you sending? xml or json?
         
        
           
         
            
       
      
        
          
          
            trance  
          
              
                March 25, 2016, 11:58am
               
              5 
           
         
        
          
I am not sending any input, I am just visiting page localhost/api/users/1 in my browser. Everything else is set up exactly like in guide, same code.
         
        
           
         
            
       
      
        
          
          
            trance  
          
              
                March 25, 2016, 12:00pm
               
              6 
           
         
        
        
           
         
            
       
      
        
        
          Try to add in your controller:
	public function behaviors()
	{
        $behaviors = parent::behaviors();
        
        $behaviors['contentNegotiator'] = [
	            'class' => 'yii\filters\ContentNegotiator',
	            'formats' => [
	                'application/json' => Response::FORMAT_JSON,
	            ]
        ];
        
        return $behaviors;
	}	
 
        
           
         
            
       
      
        
          
          
            trance  
          
              
                March 25, 2016, 12:03pm
               
              8 
           
         
        
          
Try to add in your controller:
	public function behaviors()
	{
        $behaviors = parent::behaviors();
        
        $behaviors['contentNegotiator'] = [
	            'class' => 'yii\filters\ContentNegotiator',
	            'formats' => [
	                'application/json' => Response::FORMAT_JSON,
	            ]
        ];
        
        return $behaviors;
	}	
 
 
I also had to add use yii\web\Response;
Yes now it returns JSON. But can this be configured somehow globally for all controllers ?
         
        
           
         
            
       
      
        
        
          Sure!
Create a BaseController and extend every controller in the module from that BaseController.
         
        
           
         
            
       
      
        
          
          
            trance  
          
              
                March 25, 2016, 12:07pm
               
              10 
           
         
        
          
Hmm, its strange that you can not configure this in application configuration. Thank you for your help !
         
        
           
         
            
       
      
        
          
          
            bobonov  
          
              
                March 25, 2016,  3:23pm
               
              11 
           
         
        
          The response goes as json if the client send the corresponding http header
Accept:application/json
http://www.yiiframew …formatting.html
If this header is not present the response is in xml
Rest client send this header automatically, browser no.
Thats why you get and xml accessing with a browser, try with a rest client or add the above header to your browser using an extension.
You can force json response globally in bootstrap section of your application configuration configuring the ContentNegotiator
http://www.yiiframework.com/doc-2.0/yii-filters-contentnegotiator.html#$formats-detail 
	'bootstrap' => [
    	[
        	'class' => 'yii\filters\ContentNegotiator',
        	'formats' => [
            	'application/json' => Response::FORMAT_JSON,
        	],
    	],
	],
 
        
           
         
            
       
      
        
          
          
            softark  
          
              
                March 25, 2016,  3:48pm
               
              12 
           
         
        
          [color="#006400 "]/* Moved from "General Discussions" to "REST APIs" */[/color]