Add Headers
Overview
The Add Headers policy action enables the addition of headers to both inbound requests and outbound responses.
Example
Use this action config in your Traffic Policy
- YAML
- JSON
# snippet
---
actions:
- type: "add-headers"
config:
headers:
is-ngrok: "1"
country: "${conn.geo.country_code}"
// snippet
{
"actions": [
{
"type": "add-headers",
"config": {
"headers": {
"is-ngrok": "1",
"country": "${conn.geo.country_code}"
}
}
}
]
}
Behavior
When executed as an inbound policy, this action will add headers on an incoming http request before reaching the upstream server with the configured headers. When executed as an outbound policy, the configured headers are added to the response from the upstream server.
Reference
Supported Directions
- Inbound
- Outbound
Configuration
Type |
---|
add-headers |
Parameter | Description | |
---|---|---|
headers | Map<string, string> | Headers to be added to the either the request or response. Supports interpolating the results of cel expressions in header values. |
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: "add-headers"
config:
headers:
time: "${time.now}"
// simple variable replacement
{
"actions": [
{
"type": "add-headers",
"config": {
"headers": {
"time": "${time.now}"
}
}
}
]
}
- YAML
- JSON
# using a macro
---
actions:
- type: "add-headers"
config:
headers:
x-has-foobar: "${hasReqHeader('foobar')}"
// using a macro
{
"actions": [
{
"type": "add-headers",
"config": {
"headers": {
"x-has-foobar": "${hasReqHeader('foobar')}"
}
}
}
]
}
- YAML
- JSON
# complex expression
---
actions:
- type: "add-headers"
config:
headers:
complex: "${size(getReqHeader('Content-Type')) > 0}"
// complex expression
{
"actions": [
{
"type": "add-headers",
"config": {
"headers": {
"complex": "${size(getReqHeader('Content-Type')) > 0}"
}
}
}
]
}