Add Headers
Overview
The Add Headers policy action enables the addition of headers to both inbound requests and outbound responses.
Example
Traffic Policy for adding headers to an outbound
response.
- YAML
- JSON
# snippet
---
actions:
- type: "add-headers"
config:
headers:
x-custom-header: "custom-value"
x-template-example: "started at ${conn.ts.start}"
// snippet
{
"actions": [
{
"type": "add-headers",
"config": {
"headers": {
"x-custom-header": "custom-value",
"x-template-example": "started at ${conn.ts.start}"
}
}
}
]
}
Request:
curl https://example.ngrok.app/api/v1
Result:
< HTTP/2 200
< x-custom-header: custom-value
< x-template-example: started at 2024-06-24T15:30:00Z
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.
More Examples
Simple cel expression
- YAML
- JSON
# snippet
---
actions:
- type: "add-headers"
config:
headers:
x-time: "${time.now}"
// snippet
{
"actions": [
{
"type": "add-headers",
"config": {
"headers": {
"x-time": "${time.now}"
}
}
}
]
}
Request:
curl https://example.ngrok.app/api/v1
Result:
< HTTP/2 200
< x-time: 2024-06-24T15:30:00Z
Complex cel expression
- YAML
- JSON
# complex expression
---
actions:
- type: "add-headers"
config:
headers:
x-complex: "${size(getReqHeader('Content-Type')) > 0}"
// complex expression
{
"actions": [
{
"type": "add-headers",
"config": {
"headers": {
"x-complex": "${size(getReqHeader('Content-Type')) > 0}"
}
}
}
]
}
Request:
curl https://example.ngrok.app/api/v1
Result:
< HTTP/2 200
< x-complex: true