Custom Response
Overview
The Custom Response policy action returns a hard-coded response back to the client that made a request to your endpoint.
Example
Use this action config in your Traffic Policy
- YAML
- JSON
# snippet
---
actions:
- type: "custom-response"
config:
status_code: 400
content: "{\"error\":{\"message\":\"Bad Request\"}}"
headers:
content-type: "application/json"
// snippet
{
"actions": [
{
"type": "custom-response",
"config": {
"status_code": 400,
"content": "{\"error\":{\"message\":\"Bad Request\"}}",
"headers": {
"content-type": "application/json"
}
}
}
]
}
Behavior
When executed as an inbound policy, this action bypasses the upstream server and immediately returns to the caller with the configured response. When executed as an outbound policy, the response from the upstream server is overwritten with the configured response.
If this action is executed, no further actions in the traffic policy will be executed.
Reference
Supported Directions
- Inbound
- Outbound
Configuration
Type |
---|
custom-response |
Parameter | Description | |
---|---|---|
status_code | int | The status code of the response. |
content | string | The body of the response. Supports interpolating the results of cel expressions. |
headers | Map<string, string> | Headers to be added to the response. If content-type is not included in headers , this action will attempt to infer the correct content-type . |
Templating Syntax
The results of CEL expressions can be interpolated into your policy's config
using ngrok's ${}
templating syntax. For a complete list of available variables and functions or to see a more detailed explanation, checkout the docs.
Templating Examples
- YAML
- JSON
# Simple variable replacement
---
actions:
- type: "custom-response"
config:
status_code: 200
content: "{\"connection-start\":\"${conn.start_ts}\"}"
headers:
content-type: "application/json"
// Simple variable replacement
{
"actions": [
{
"type": "custom-response",
"config": {
"status_code": 200,
"content": "{\"connection-start\":\"${conn.start_ts}\"}",
"headers": {
"content-type": "application/json"
}
}
}
]
}
- YAML
- JSON
# Multiple inline expressions
---
actions:
- type: "custom-response"
config:
status_code: 418
content: "connection began at ${conn.start_ts}, now ${timestamp(time.now)}"
headers:
content-type: "text/plain"
// Multiple inline expressions
{
"actions": [
{
"type": "custom-response",
"config": {
"status_code": 418,
"content": "connection began at ${conn.start_ts}, now ${timestamp(time.now)}",
"headers": {
"content-type": "text/plain"
}
}
}
]
}