Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compressing the response payload in Django REST?

I was wondering: would it be possible to compress the response payload in Django REST?

At the moment, the response payloads are plain JSON data. However, there's quite a lot of data to bounce back and forth so I was wondering if compressing the data would help with the bandwidth issues.

like image 573
manabreak Avatar asked Apr 23 '15 12:04

manabreak


People also ask

How do I compress a REST API response?

To use compression, include the HTTP header Accept-Encoding: gzip or Accept-Encoding: deflate in a request. The REST API compresses the response if the client properly specifies this header. The response includes the header Content-Encoding: gzip or Accept-Encoding: deflate .

How do I reduce API payload size?

You can enable or disable compression for an API by using the API Gateway console, the AWS CLI, or the API Gateway REST API. If you want the compression applied on a payload of any size, set the minimumCompressionSize value to zero. However, compressing data of a small size might actually increase the final data size.

How do I compress a response?

When a client can process compressed content, the client must inform the server of its capabilities by sending the Accept-Encoding header with the request. When a server sends compressed content, it must include information in the Content-Encoding header on how the compressed response is encoded.

Should I compress API response?

Use a compression header to compress a REST API request or response. Compression reduces the bandwidth required for a request, although it requires more processing power at your client. In most cases, this tradeoff benefits the overall performance of your application.


2 Answers

HTTP response compression will most likely not be handled by Django but by your HTTP server using the gzip or deflate algorithms.

You just need to make sure your HTTP server is configured to compress HTTP Responses with Content-Type header set to application/json.

How to enable gzip compression for nginx: https://rtcamp.com/tutorials/nginx/enable-gzip/

like image 78
aumo Avatar answered Sep 21 '22 06:09

aumo


The following worked for me.

I actually turned gzip on at the nginx level, not within Django or Django Rest Framework.

/etc/nginx/nginx.conf file:

    http {

        #... other settings ...#

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    }

This leaves the compressing up to the nginx server and as most modern browsers automatically know how to extract (uncompress) gzip compression, I didn't need to do anything on my client-side - even when receiving json data inside an Angular spa app.

My 1.3 MB JSON payload turned into about a 180 KB payload.

A pretty quick and fast way to save MB's of data.

like image 41
aero Avatar answered Sep 22 '22 06:09

aero