/ yii2

Yii2 headers not changed

Spent some time wondering what was wrong with my code until I found this: http://stackoverflow.com/questions/33106660/http-headers-are-not-being-changed-yii2

Had been trying

$r = \Yii::$app->response;
$r->headers->set( 'Content-Type', 'application/json; charset=UTF-8' );

The header stubbornly wouldn't change to application/json no matter what.

Turns out you have to set it to the raw format before you're able to change it.
So the below works:

$r = \Yii::$app->response;
$r->format = \yii\web\Response::FORMAT_RAW;
$r->headers->set( 'Content-Type', 'application/json; charset=UTF-8' );
Yii2 headers not changed
Share this