If you run any web service always enable support for serving compressed responses. It will save egress bandwidth costs for you. And, more importantly, for your users. Over time, the servers as well as client devices have become more powerful, so, compressing/decompressing data on the fly is cheap.

For example,

Bash
1
2
3
4
5
6
$ curl https://ashishb.net/ > tmp1.htm && du -shc tmp1.htm
32K
$ curl -H 'Accept-encoding: gzip' https://ashishb.net > tmp1.gz && du -shc tmp1.gz
12K
# curl can automatically decompress the response as well
$ curl --compressed https://ashishb.net

To support this on your servers, handle Accept-encoding. In many cases, you might find standard libraries that support this. For example, for the Go language’s popular Gin framework, it is possible to enable this via middleware.

Here’s a self-contain example demonstrating the same.

Go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Save this file as server.go
// Then one-time setup
// $ go mod init tmp1 && go mod tidy
// Usage: go run server.go
package main

import (
  "net/http"

  "github.com/gin-contrib/gzip"
  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()
  r.Use(gzip.Gzip(gzip.DefaultCompression))
  r.GET("/ping", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "message": "pong",
    })
  })
  r.Run()
}

Now

Bash
1
2
3
4
5
6
# This will produce plain-text output
$ curl localhost:8080/ping
# This will produce compressed binary output
$ curl -H 'Accept-encoding: gzip' localhost:8080/ping -o /dev/stdout
# The compressed binary output can be decoded via the following
$ curl -H 'Accept-encoding: gzip' localhost:8080/ping | gunzip -

Note: if you are using Ngnix, you can enable this by adding the following to your Nginx config file

Ngnix
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
...
http {
  ...
  server {
    ...
    gzip on;
    gzip_min_length 1000;
    ...
  }
}