Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is gzip encoding compatible with JSON?

I'm trying to narrow down some weirdness going on with my AJAX calls. My PHP scripts have this at the start:

ob_start("ob_gzhandler");

Works great with HTML. But are there any problems with doing it with application/json data? Any browser issues anyone is aware of?

like image 824
Jordie Avatar asked Mar 18 '09 13:03

Jordie


People also ask

Can I gzip JSON?

Compressing with gzip As text data, JSON data compresses nicely. That's why gzip is our first option to reduce the JSON data size. Moreover, it can be automatically applied in HTTP, the common protocol for sending and receiving JSON.

Can you compress JSON response?

JSON responses are not natively compressed. Response compression can optimize network bandwidth usage and increase application responsiveness.

Should JSON be compressed?

Though JSON is a concise format, it is also better used over a slow network in compressed mode.

What is accept-Encoding gzip?

The Accept-Encoding header is used for negotiating content encoding. Accept-Encoding: gzip, deflate. The server responds with the scheme used, indicated by the Content-Encoding response header. Content-Encoding: gzip. Note that the server is not obligated to use any compression method.


2 Answers

I don't think so... I've used static files stored as gzipped JSON before, and it worked fine with AJAX.

edit: I checked my php script and the only thing special I did was to include these headers:

Content-Encoding: gzip
Content-Type: text/plain

If I remember right, whenever I tried to change the Content-Type to something that would indicate JSON, the client had trouble.

like image 107
Jason S Avatar answered Oct 14 '22 18:10

Jason S


Some older browsers, like certain versions of IE6, screw up gzipped content, especially js content.

Just check that your server sends proper content-encoding header, that is

Content-Encoding: gzip

You should also check the headers sent by the browser for proper accept-encoding header before sending gzipped content... that is,

Accept-Encoding: gzip,deflate
like image 23
kkyy Avatar answered Oct 14 '22 18:10

kkyy