LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub

Response Encode and Request Decode

To reduce bandwidth usage and speed up the delivery, many clients and servers utilize gzip or deflate encoding for large requests or responses.

One of our users built an API that receives batch data from iOS, and the request is encoded with gzip. It requires that we have a RequestDecodeHandler before the BodyHandler to decode the gzip stream so that subsequent handlers can handle the request as a normal JSON body.

Sometimes, we might have services that produce big size JSON responses to the client, and we might consider encoding the response body with gzip as the majority of browsers support it.

This module contains two middleware handlers one works on the incoming request and the other works on the outgoing response. Usually, there is only one handler that should be used by a particular service.

RequestDecodeHandler

This middleware handler must be wired in the request handler chain before BodyHandler. It looks for the header for encoding and decoding the gzip and deflating the stream to normal JSON for subsequent handlers to process.

A typical request will have headers like this.

{content-encoding=[gzip], accept-encoding=[gzip], content-type=[application/json] ...

If your service has clients sending both encoded or non-encoded requests, you can safely wire in this handler as it will check the header and decode when necessary.

After wiring in this handle in service.yml or handler.yml, you need to add a configuration file request-decode.yml to the config folder.

Here is the content for most applications that need this handler.

---
# request decode handler for gzip and deflate
enabled: true
decoders:
  - gzip
  - deflate

ResponseEncodeHandler

This middleware handler can be wired in anywhere in the chain as it works on the response only. It looks for the header for the encoding requirement and picks up one of the providers: gzip or deflate.

The client request must have a header called accept-encoding=gzip or accept-encoding=deflate.

If requests doen’t contain above header, then the normal stream will be sent.

After wiring in this handler in service.yml or handler.yml, you need to add a configuration file response-encode.yml to the config folder.

Here is the content for most applications that need this handler.

---
# response encode handler for gzip and deflate
enabled: true
encoders:
  - gzip
  - deflate
  • About Light
    • Overview
    • Testimonials
    • What is Light
    • Features
    • Principles
    • Benefits
    • Roadmap
    • Community
    • Articles
    • Videos
    • License
    • Why Light Platform
  • Getting Started
    • Get Started Overview
    • Environment
    • Light Codegen Tool
    • Light Rest 4j
    • Light Tram 4j
    • Light Graphql 4j
    • Light Hybrid 4j
    • Light Eventuate 4j
    • Light Oauth2
    • Light Portal Service
    • Light Proxy Server
    • Light Router Server
    • Light Config Server
    • Light Saga 4j
    • Light Session 4j
    • Webserver
    • Websocket
    • Spring Boot Servlet
  • Architecture
    • Architecture Overview
    • API Category
    • API Gateway
    • Architecture Patterns
    • CQRS
    • Eco System
    • Event Sourcing
    • Fail Fast vs Fail Slow
    • Integration Patterns
    • JavaEE declining
    • Key Distribution
    • Microservices Architecture
    • Microservices Monitoring
    • Microservices Security
    • Microservices Traceability
    • Modular Monolith
    • Platform Ecosystem
    • Plugin Architecture
    • Scalability and Performance
    • Serverless
    • Service Collaboration
    • Service Mesh
    • SOA
    • Spring is bloated
    • Stages of API Adoption
    • Transaction Management
    • Microservices Cross-cutting Concerns Options
    • Service Mesh Plus
    • Service Discovery
  • Design
    • Design Overview
    • Design First vs Code First
    • Desgin Pattern
    • Service Evolution
    • Consumer Contract and Consumer Driven Contract
    • Handling Partial Failure
    • Idempotency
    • Server Life Cycle
    • Environment Segregation
    • Database
    • Decomposition Patterns
    • Http2
    • Test Driven
    • Multi-Tenancy
    • Why check token expiration
    • WebServices to Microservices
  • Cross-Cutting Concerns
    • Concerns Overview
  • API Styles
    • Light-4j for absolute performance
    • Style Overview
    • Distributed session on IMDG
    • Hybrid Serverless Modularized Monolithic
    • Kafka - Event Sourcing and CQRS
    • REST - Representational state transfer
    • Web Server with Light
    • Websocket with Light
    • Spring Boot Integration
    • Single Page Application
    • GraphQL - A query language for your API
    • Light IBM MQ
    • Light AWS Lambda
    • Chaos Monkey
  • Infrastructure Services
    • Service Overview
    • Light Proxy
    • Light Mesh
    • Light Router
    • Light Portal
    • Messaging Infrastructure
    • Centralized Logging
    • COVID-19
    • Light OAuth2
    • Metrics and Alerts
    • Config Server
    • Tokenization
    • Light Controller
  • Tool Chain
    • Tool Chain Overview
  • Utility Library
  • Service Consumer
    • Service Consumer
  • Development
    • Development Overview
  • Deployment
    • Deployment Overview
    • Frontend Backend
    • Linux Service
    • Windows Service
    • Install Eventuate on Windows
    • Secure API
    • Client vs light-router
    • Memory Limit
    • Deploy to Kubernetes
  • Benchmark
    • Benchmark Overview
  • Tutorial
    • Tutorial Overview
  • Troubleshooting
    • Troubleshoot
  • FAQ
    • FAQ Overview
  • Milestones
  • Contribute
    • Contribute to Light
    • Development
    • Documentation
    • Example
    • Tutorial
“Response Encode and Request Decode” was last updated: April 5, 2021: Issue246 (#256) (50b1c10)
Improve this page
  • News
  • Docs
  • Community
  • Reddit
  • GitHub
  • About Light
  • Getting Started
  • Architecture
  • Design
  • Cross-Cutting Concerns
  • API Styles
  • Infrastructure Services
  • Tool Chain
  • Utility Library
  • Service Consumer
  • Development
  • Deployment
  • Benchmark
  • Tutorial
  • Troubleshooting
  • FAQ
  • Milestones
  • Contribute