How to define content-type with Code Igniter REST_Controller

codeigniter, content-type, json, rest

Solution

I'm not sure if the config sets the format. But a simple work around might be just to use the output class to set the header content type, something like:

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

(Taken from the manual: here)

Problem

I'm working with the REST_Controller extended CI_Controller and for some reason my requests are all coming back with a content-type of text/html instead of json. In my config, I have json set as the default format: ``` $config['rest_default_format'] = 'json'; ``` My results are coming back as JSON, but the content-type isn't being set. Can anyone help with what I'm missing?

Original source