REST API returning compressed XML but not JSON

I have implemented a REST API using Yii 2.0.8. The client can request JSON or XML response format either by using the Accept HTTP header or through a parameter in the URL. Likewise, either by using Accept-encoding:gzip or a URL parameter, the client can request the response to be gzipped. It all works beautifully with the exception that XML is properly gzipped while JSON is alwasy returned uncompressed.

There is a bug report on GitHub (#8331) just one year old without any follow up.

I could look into the code myself, but I cannot find where the compression is performed.

Any hints?

Thank’s to an illuminating email from the Yii2 guys, I was able to fix my problem. I was told that the compression of the response was handled by the webserver and not by Yii2 (that explains why I could not find where it was being done…).

I just added the following line to my Apache configuration

AddOutputFilterByType DEFLATE application/json application/xml

and everything now works like a charm!