Generate Response Processor
Overview
The GenerateResponse
processor allows users to define custom responses for incoming requests. This processor is useful for handling predefined responses without forwarding the request to a backend service, enabling mock responses, API testing, and quick feedback loops.
Input and Output
This processor operates between the request and response streams:
-
Input Stream:
Request
– Intercepts requests before they reach the provider. -
Output Stream:
Response
– Generates and returns a response directly to the client.
By resolving requests within the flow, the GenerateResponse
processor prevents further processing by downstream processors or external services.
Parameters
Each parameter is defined as a key-value pair inside the parameters
section.
status
Type: string
Required: False
Default: 429
Defines the HTTP status code to return in the response.
Example:
- key: status
value: "429"
body
Type: string
Required: False
Default: Quota Exceeded. Please try again later.
Defines the body content of the response.
Example:
- key: body
value: "Quota Exceeded. Please try again later."
Content-Type
Type: string
Required: False
Default: text/plain
Specifies the content type of the response. The content-type can be application/json
or text/plain
.
Example:
- key: Content-Type
value: "application/json"
If your content type is application/json
make sure the body is in the correct json format with necessary escape characters
Best Practices
- Use
status
to return appropriate HTTP status codes based on the request scenario. - Ensure
body
provides meaningful feedback to the client. - Set
Content-Type
correctly to match the expected format of the response.
For more examples on how to use Generate Response, please see the Limiter Flow or the Retry Flow.
Generate Response Processor Template
GenerateResponse:
processor: GenerateResponse
parameters:
- key: status
value: "429" # HTTP status code
- key: body
value: "Too Many Requests. Please try again later." # Response body
- key: Content-Type
value: "text/plain" # Response content type
Use Case
Use the GenerateResponse
processor to return predefined error messages for missing resources:
ErrorResponse:
processor: GenerateResponse
parameters:
- key: status
value: "404"
- key: body
value: "Resource not found"
- key: Content-Type
value: "text/plain"
The above configuration will return a 404 response with the body { "message": "Resource not found" }
.