Skip to content

Gzip Response

Learn how to send Gzip compressed responses in Iris.

Basic Gzip Response

go
package main

import "github.com/kataras/iris/v12"

func main() {
    app := iris.New()

    // Enable gzip compression
    app.Use(iris.Compression)

    app.Get("/", func(ctx iris.Context) {
        largeData := getLargeData()
        // Response will be automatically compressed
        ctx.JSON(largeData)
    })

    app.Listen(":8080")
}

Custom Compression Options

go
app.Use(iris.Compression, iris.CompressionOptions{
    // Compression level (1-9)
    Level: 6,
    
    // Minimum content length to trigger compression
    MinLength: 256,
    
    // Exclude specific content types
    Exclude: []string{
        "image/jpeg",
        "image/png",
    },
})

Per-Route Compression

go
app.Get("/compressed", iris.Compression, func(ctx iris.Context) {
    // This route will be compressed
    largeData := getLargeData()
    ctx.JSON(largeData)
})

app.Get("/uncompressed", func(ctx iris.Context) {
    // This route will not be compressed
    data := getData()
    ctx.JSON(data)
})

Best Practices

  1. Configuration:

    • Set appropriate level
    • Configure thresholds
    • Exclude file types
    • Monitor compression
    • Handle errors
  2. Performance:

    • Monitor ratios
    • Cache compressed
    • Handle timeouts
    • Optimize memory
    • Regular cleanup
  3. Headers:

    • Set content encoding
    • Handle vary header
    • Configure caching
    • Monitor client support
    • Handle errors
  4. Maintenance:

    • Monitor usage
    • Update settings
    • Regular testing
    • Document changes
    • Review performance

Built with excellence by Hellenic Development, delivering enterprise-grade solutions.