Skip to main content


This middleware take the preferredEncoding output from @middy/http-content-negotiation and applies the encoding to response.body when a string.


To install this middleware you can use NPM:

npm install --save @middy/http-content-encoding


  • br (object) (default {}): zlib.createBrotliCompress brotliOptions
  • gzip (object) (default {}): zlib.createGzip gzipOptions
  • deflate (object) (default {}): zlib.createDeflate deflateOptions
  • overridePreferredEncoding (array[string]) (optional): Override the preferred encoding order, most browsers prefer gzip over br, even though br has higher compression. Default: []


  • Important For br encoding NodeJS defaults to 11. Levels 10 & 11 have been shown to have lower performance for the level of compression they apply. Testing is recommended to ensure the right balance of compression & performance.

Sample usage

import middy from '@middy/core'
import httpContentNegotiation from '@middy/http-content-negotiation'
import httpContentEncoding from '@middy/http-content-encoding'
import { constants } from 'zlib'

const handler = middy((event, context) => {
return {
statusCode: 200,
body: '{...}'

br: {
params: {
[constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT, // adjusted for UTF-8 text
overridePreferredEncoding: ['br', 'gzip', 'deflate']

export default { handler }