Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I put $this->request->headers('Content-Type', 'application/json');

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'); ?

like image 429
T.i. Avatar asked Jun 04 '26 00:06

T.i.


1 Answers

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));
like image 107
Andrew Schmid Avatar answered Jun 05 '26 14:06

Andrew Schmid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!