惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
C
Check Point Blog
B
Blog RSS Feed
G
Google Developers Blog
H
Help Net Security
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Recent Announcements
Recent Announcements
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
DataBreaches.Net
小众软件
小众软件
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
T
Tailwind CSS Blog
J
Java Code Geeks
MyScale Blog
MyScale Blog
雷峰网
雷峰网
有赞技术团队
有赞技术团队
博客园 - 聂微东

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Best practices for using Python & uv inside Docker Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker
Always support compressed response in an API service
Ashish Bhatia · 2024-03-10 · via ashishb.net

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,

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.

 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

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

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