Skip to content
Created by

Serialization & compression

Connect can work with any schema definition language, but it comes with built-in support for Protocol Buffers. Because the Protocol Buffer specification includes mappings to and from JSON, any Connect API defined with a Protobuf schema also supports JSON. This is especially convenient for web browsers and ad-hoc debugging with cURL.

Connect handlers automatically accept JSON-encoded requests — there’s no special configuration required. Connect clients default to using binary Protobuf. To configure your client to use JSON instead, pass codec=proto_json_codec() during client construction.

Connect clients and handlers support compression. Usually, compression is helpful — the small increase in CPU usage is more than offset by the reduction in network I/O.

In particular, Connect encourages asymmetric compression: clients can send uncompressed requests while asking for compressed responses. Because responses are usually larger than requests, this approach compresses most of the data on the network without requiring the client to make any assumptions about the server.

By default, Connect handlers and clients support gzip compression using the standard library’s gzip at compression level 6, and clients send gzip-compressed requests. To send uncompressed requests, pass send_compression=None during client construction.

Connect also provides brotli and zstd compression in connectrpc.compression.brotli and connectrpc.compression.zstd, which need the brotli and zstandard packages installed. Pass them to handlers with compressions=, and to clients with accept_compression= and send_compression=:

from connectrpc.compression.gzip import GzipCompression
from connectrpc.compression.zstd import ZstdCompression
app = GreetServiceASGIApplication(
Greeter(),
compressions=[ZstdCompression(), GzipCompression()],
)
client = GreetServiceClient(
"http://localhost:8000",
accept_compression=[ZstdCompression(), GzipCompression()],
send_compression=ZstdCompression(),
)

It’s worth noting that clients using the Connect protocol for unary RPCs ask for compressed responses using the Accept-Encoding HTTP header. This matches standard HTTP semantics, so browsers can easily make efficient Connect RPCs: they automatically ask for compressed responses, and the network inspector tab automatically decompresses the data if necessary. Connect’s Accept-Encoding support also works well with cURL’s --compressed flag.