Skip to main content

REST request and response compression

Kreya can compress the request body before sending it and automatically decompresses compressed response bodies. The on-the-wire size of compressed responses is displayed in the UI.

Compressing the request body

Request compression is opt-in. To compress the body of a REST request, add a Content-Encoding header to the operation and set it to one of the supported encodings:

ValueAlgorithm
gzipGZIP (RFC 1952)
deflateDEFLATE / zlib (RFC 1950, 1951)
brBrotli (RFC 7932)

Kreya compresses the body just before sending it. The Content-Encoding header is preserved on the request, so the server knows how to decode it.

If the header is missing or set to an unsupported value, the request body is sent uncompressed.

Decompressing the response body

If the server returns a response with a supported Content-Encoding header (gzip, deflate or br), Kreya transparently decompresses the body while it is read. Scripts and the response view always see the decompressed content; the original Content-Encoding response header is left untouched.

Responses with an unsupported or missing Content-Encoding are passed through as-is.

Response size in the UI

When a response is compressed, the response view shows both the decompressed body size and the on-the-wire (compressed) size, along with the compression ratio. Uncompressed responses only show a single size.

Accessing size information from scripts

The size information is also available on the script API via call.response.sizeInfo. For compressed responses, the compressedBodySize, compressedSize, encoding and compressionRatio fields are populated; for uncompressed responses they are undefined.

kreya.rest.onCallCompleted(call => {
const size = call.response.sizeInfo;

kreya.trace(`Body size (decompressed): ${size.bodySize} bytes`);

if (size.encoding) {
kreya.trace(
`Compressed with ${size.encoding}: ${size.compressedBodySize} bytes on the wire ` +
`(ratio ${(size.compressionRatio * 100).toFixed(1)}%)`,
);
}
});

See the REST script API reference for the full list of fields.