This documentation uses Gloo Gateway proxies with the Kubernetes Gateway API. To use the Edge API instead, click here. Note that some features, such as AI gateways, are not supported with the Edge API.
Add headers to body
Use extractors to capture request header values and add those values to the request body.
The following example walks you through how to use extractors to extract request header values by using regular expressions. The captured header values are then added to the response body by using the mergeExtractorsToBody setting. You can use the extractors to also indicate where you want to place the request header values in the body.
info
This guide uses a subset of the supported transformation template attributes. To review all the attributes that you can set, see Templating language.
Create a RouteOption or VirtualHostOption resource with your transformation rules. In the following example, you extract the root and nested request headers and add them to the response body by using the mergeExtractorsToBody setting. The dot notation that you use for the extractor names determines the placement of the header in the body. For example, if no dot notation is used, such as in root, the header is added to the body’s root level. If dot notation is used, such as in payload.nested, the extractor is added under the payload.nested field.
kubectl apply -f- <<EOF
apiVersion: gateway.solo.io/v1
kind: RouteOption
metadata:
name: transformation
namespace: httpbin
spec:
targetRefs:
- group: gateway.networking.k8s.io
kind: HTTPRoute
name: httpbin
options:
transformations:
requestTransformation:
transformationTemplate:
# Merge the specified extractors to the request body
mergeExtractorsToBody: {}
extractors:
# The name of this attribute determines where the value will be nested in the body.
# Because no dots are specified, such as root.nested, the root header value is added to the body's root level.
root:
# Name of the header to extract
header: 'root'
# Regex to apply to it. This value is required and is configured to capture the entire header.
regex: '.*'
# The name of this attribute determines where the value will be nested in the body.
# Because dot notation is used, the nested header is placed under the placeholder.nested field in the body.
payload.nested:
# Name of the header to extract
header: 'nested'
# Regex to apply to it. This value is required and is configured to capture the entire header.
regex: '.*'
EOF
kubectl apply -n gloo-system -f- <<EOF
apiVersion: gateway.solo.io/v1
kind: VirtualHostOption
metadata:
name: transformation
namespace: gloo-system
spec:
options:
transformations:
requestTransformation:
transformationTemplate:
# Merge the specified extractors to the request body
mergeExtractorsToBody: {}
extractors:
# The name of this attribute determines where the value will be nested in the body.
# Because no dots are specified, such as root.nested, the root header value is added to the body's root level.
root:
# Name of the header to extract
header: 'root'
# Regex to apply to it. This value is required and is configured to capture the entire header.
regex: '.*'
# The name of this attribute determines where the value will be nested in the body.
# Because dot notation is used, the nested header is placed under the placeholder.nested field in the body.
payload.nested:
# Name of the header to extract
header: 'nested'
# Regex to apply to it. This value is required and is configured to capture the entire header.
regex: '.*'
targetRefs:
- group: gateway.networking.k8s.io
kind: Gateway
name: http
namespace: gloo-system
EOF
Send a request to the httpbin app and include the root and nested request headers. Verify that you get back a 200 HTTP response code and that the value of the root header is added to the body’s root level, and that the nested header value is added under the payload.nested field in your response.