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
.

- 1,099
- 1
- 10
- 21
-
is your application on utf8? via core.php? then it should do that by default. – mark Nov 16 '12 at 21:59
-
Yes it is set to `'App.encoding', 'UTF-8'`. – FrediWeber Nov 16 '12 at 22:01
-
I just found out that every normal view send the header. The JSON view is the only view that doesn't send the correct header. – FrediWeber Nov 16 '12 at 22:04
-
Why are you setting it in the view and not in the controller which you have to pass anyway? – func0der Nov 16 '12 at 22:18
-
Sorry but I don't understand your question. I don't set a header in the view. The header is set by default in every normal view except the JSON view. – FrediWeber Nov 16 '12 at 22:29
6 Answers
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...

- 21,691
- 3
- 49
- 71
-
why looking for a workaround? either googling and finding out that other types have a "right" to also be appended with the charset and making this an official cakephp bug - or realizing that cakephp actually sets the headers properly according to the HTTP spec. both is possible. – mark Nov 16 '12 at 22:43
-
1PS: My googling revealed: http://stackoverflow.com/questions/9254891/what-does-content-type-application-json-charset-utf-8-really-mean => "redundancy" for json anyway. about other types I don't know. – mark Nov 16 '12 at 22:45
-
for clarification: "Content-Type: application/json; charset=UTF-8" has been added to the core as fix for this in the current master branch: https://github.com/cakephp/cakephp/commit/9b479958f613e2ce1d290f26d246e76313e821f2 - the issue is officially solved – mark Nov 21 '12 at 13:29
-
Funny, I had this same problem just last night. It seems that UTF-8 is the default JSON encoding and that's probably why the header isn't sent by Cake (but I couldn't find reference to this decision either way).
Solution for me was to use a php's json_decode()
-- it should decode the string properly. I had lots of French accents showing up as java/C unicode, e.g. \u00E9
and thought it was a header problem. But after running the string through json_decode()
the accented characters all showed correctly.
Marks' link the comments has good info too.

- 4,851
- 33
- 30
application/json doesn't have a charset parameter. There's no point in trying to set one. See http://rfc7159.net/rfc7159#ianacons.

- 40,156
- 8
- 95
- 98
I´m using cake 2.10.11 and just used this code in the json layout:
$this->response->charset('utf-8');

- 182
- 1
- 10
You can do it by using $this->RequestHandler->ext = 'json';
in your beforefilter method
check out the following example
public function beforeFilter(){
parent::beforeFilter();
$this->RequestHandler->ext = 'json';
}
public function index()
{
$this->autoRender = false;
$output = array(
array('value' =>'first value'),
array('value' =>'second value'),
);
$json = json_encode($output);
$this->response->body($json);
}

- 842
- 9
- 21