How to determine content type using Code Igniter REST_Controller

I am working with the extended CI_Controller REST_Controller and for some reason my requests are returned with the content type text / html instead of json. In my configuration, I set json as the default format:

$config['rest_default_format'] = 'json'; 

My results are returned as JSON, but the content type is not set. Can someone help with what I am missing?

+6
source share
2 answers

I am not sure if config sets the format. But a simple job can only be to use the output class to set the content type of the header, for example:

 $this->output ->set_content_type('application/json') ->set_output(json_encode(array('foo' => 'bar'))); 

(taken from the manual: here )

+11
source

When setting the contect_type parameter for each function, this can be made common at the controller level by setting it in the constructor.

 public function __construct() { parent::__construct(); ... $this->output->set_content_type('application/json'); } 

So you just set the output at each function level

 $this->output->set_output('{"message":"Failure"}'); 

It worked for me.

0
source

Source: https://habr.com/ru/post/917129/


All Articles