Serializing Doctrine objects to proper JSON content type using ResponseTrait - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11) +--- Thread: Serializing Doctrine objects to proper JSON content type using ResponseTrait (/showthread.php?tid=82175) |
Serializing Doctrine objects to proper JSON content type using ResponseTrait - b126 - 06-21-2022 Dear all, I'm trying to return Doctrine serialized objects (thanks to JMS Serializer) as JSON. I am extending the ResourceController (and then using ResponseTrait) The problem is that the returned headers are of type "text/html" instead of "application/json" when i use Respond. If I use the more classic Response->setJSON, then the header is fine. If I well understand, Respond will always consider returning text/html when receiving a string as argument. How to get around this? PHP Code: class Employees extends \CodeIgniter\RESTful\ResourceController Thank you RE: JSON content type is missing when using respond - b126 - 06-21-2022 Well, I think I get a workaround. Using Serializer.toArray(); instead of Serializer.serialize(); will do the trick. PHP Code: $res = $serializer->toArray(getEmployeeRepo()->findAll()); RE: JSON content type is missing when using respond - iRedds - 06-21-2022 $this->respond($res) sets the Content-Type: text/html header because $res is a string. |