I'm trying to change the content-type to application/json in Kohana. I put this in an action in my controller:
$this->request->headers('Content-Type', 'application/json');
$this->content = json_encode($json_data);
The request however, has still the text/html content-type.
Where should I put $this->request->headers('Content-Type', 'application/json'); ?
To elaborate on Claudio's answer, yes you need to set the response header, not the request, like so
$this->response->headers('Content-Type','application/json');
Also, I'm not sure how you've implemented your controller, but it looks like it may be a template controller based on
$this->content = json_encode($json_data);
If you are using a template controller, make sure you set auto_render to FALSE.
Finally, set the response body with your json data
$this->response->body(json_encode($json_data));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With