Search code examples
phphttpcakephpheader

Set Content-Type header in CakePHP with JSON


I need to set Content-Type: application/json; charset=UTF-8 in a data view from CakePHP. I've already tried to set $this->response->header('Content-Type', 'application/json; charset=UTF-8'); but that doesn't change anything. It still just output Content-Type: application/json.


Solution

  • its in the code (CakeResponse, line 447):

    if (strpos($this->_contentType, 'text/') === 0) {
        $this->header('Content-Type', "{$this->_contentType}; charset={$this->_charset}");
    } else {
        $this->header('Content-Type', "{$this->_contentType}");
    }
    

    so only for "text/..." the charset will be appended. I dont know why, though...