This repository contains a proof of concept (POC) demonstrating how to implement response compression (Gzip and Brotli) in Azure Functions (In-Process model). It enables HTTP-triggered functions to return compressed responses based on the Accept-Encoding header.
Optimize Azure Function responses by reducing payload sizes, improving transfer speeds, and supporting clients with modern compression algorithms.
- β In-process Azure Functions using .NET 8
- β Supports Gzip and Brotli compression
- β
Detects
Accept-Encodingfrom incoming requests - β Applies dynamic compression on JSON/text payloads
- β Uses Newtonsoft.Json for serialization
- β Lightweight and extensible structure
ResponseCompression/
βββ Properties/
β βββ launchSettings.json
β βββ serviceDependencies.json
βββ Extensions/
β βββ AzureFunctionsInProcessCompressionExtensions.cs
βββ Functions/
β βββ CompressedJsonFunction.cs
βββ GlobalUsings.cs
βββ host.json
βββ local.settings.json
βββ .gitignore
βββ README.md
- Azure Functions (In-Proc)
- .NET 8
- System.IO.Compression
- Newtonsoft.Json
func startBy default, the function runs on:
http://localhost:7169
curl -H "Accept-Encoding: gzip" http://localhost:7169/api/compressed-json --output response.gzcurl -H "Accept-Encoding: br" http://localhost:7169/api/compressed-json --output response.brcurl http://localhost:7169/api/compressed-jsonCompressedJsonFunctionreceives the HTTP request- Reads the
Accept-Encodingheader - Serializes the response using
Newtonsoft.Json - Applies compression (Gzip or Brotli) using
System.IO.Compression - Sets appropriate
Content-EncodingandContent-Typeheaders - Returns the compressed stream as the response
β‘οΈ Compression logic is encapsulated in:
Extensions/AzureFunctionsInProcessCompressionExtensions.cs
- Azure Functions HTTP Trigger
- GzipStream Documentation
- BrotliStream Documentation
- Newtonsoft.Json Documentation
- Designed for the in-process model only
- Not suitable for binary or large media content
- ASP.NET Core middleware pipeline is not available in in-proc model
Feel free to fork, extend, and improve this POC. Pull requests are welcome!