Reponse setHeaders is scrubbing setContentType.
Expected:
I expected the reponse->setHeaders to merge with what was already in headers.
Actual :
I had response->setContentType('application/json') before response->setHeaders. The result meant I was losing the desired content type and it was reverting back to text/html.
Working:
$this->response->setHeaders($headers);
$this->response->setContentType('application/json', 'UTF-8');
Not working:
$this->response->setContentType('application/json', 'UTF-8');
$this->response->setHeaders($headers);
I recommend that this be added to the Phalcon 4 milestone with merging on by default. If it is added in Phalcon 3 then merging will need to be off by default. It can be currently worked around by extending the Response class and calling getHeaders in setHeaders.
@linxlad
Well not sure what you exactly do further but $this->response->setContentType('application/json', 'UTF-8'); is most likely not needed just do:
$model = $model->toArray();
return $this->request->setJsonContent($model);
Addressed in https://github.com/phalcon/cphalcon/pull/13669
Thank you @linxlad
Most helpful comment
I recommend that this be added to the Phalcon 4 milestone with merging on by default. If it is added in Phalcon 3 then merging will need to be off by default. It can be currently worked around by extending the Response class and calling
getHeadersinsetHeaders.