Configuration Reference
The Traffic Policy configuration reference for this action.Supported Phases
on_http_request
, on_http_response
Type
custom-response
Configuration Fields
Status code of the custom response being sent back.
Body of the custom response being sent back.
Map of key-value headers of the custom response to be sent back. If
content-type
is not included in headers
, this action will attempt to
infer the correct content-type
. Maximum properties 10
.
Behavior
If this action is executed, no subsequent actions in your traffic policy will be executed.on_http_request
Usage
When used during the on_http_request
phase, this action bypasses the upstream server and immediately returns the configured response to the caller.
Keep in mind that because this action bypasses the upstream server, request bodies will not be forwarded and will not be visible within the ngrok Traffic Inspector.
on_http_response
Usage
When used during the on_http_response
, this action overwrites the response from the upstream server with the configured response.
Inferring Content-Type
If thecontent-type
header is not explicitly specified in headers
, this action will attempt to infer the correct content-type
based on the provided content.
Examples
Custom HTML Maintenance Page
The following Traffic Policy configuration demonstrates how to use thecustom-response
action to return a
custom HTML maintenance page back for all requests to your endpoint.
Example Traffic Policy Document
Example Request
Custom Response for Internal Endpoint Downtime
The following Traffic Policy configuration demonstrates how to use thecustom-response
action to redirect to a URL if an internal endpoint is returning an error.
Example Traffic Policy Document
Custom JSON API Response with CEL Interpolation
The following Traffic Policy configuration demonstrates how to use thecustom-response
action to return a
JSON response with CEL Interpolation for the connection start time.
Example Traffic Policy Document
Example Request
/api/example
, ngrok returns a
custom JSON response with the default status code of 200
. The response includes a
content-type: application/json
header and a JSON body that uses CEL
Interpolation to show the connection start time using the conn.ts.start
variable.
Custom Plaintext Response with Multiple CEL Interpolations
The following Traffic Policy configuration demonstrates how to use thecustom-response
action to return a
text/plain
response with multiple CEL interpolations.
Example Traffic Policy Document
Example Request
/api/example
, ngrok returns a
custom plain text response with a status code of 418
. The response includes a
content-type: text/plain
header and a body that uses multiple string
interpolations to show the connection start time and the current time.
Action Result Variables
The following variables are made available for use in subsequent expressions and CEL interpolations after the action has run. Variable values will only apply to the last action execution, results are not concatenated.This action does not set any variables after it has been executed.