Skip to content

Add headers to body

Page as Markdown

Use extractors to capture request header values and add those values to the request body.

The following example shows you 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 also use the extractors to indicate where you want to place the request header values in the body.

Before you begin

  1. Follow the Get started guide to install Solo Enterprise for kgateway.

  2. Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.

  3. Get the external address of the gateway and save it in an environment variable.

    export INGRESS_GW_ADDRESS=$(kubectl get svc -n kgateway-system http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}")
    echo $INGRESS_GW_ADDRESS  
    kubectl port-forward deployment/http -n kgateway-system 8080:8080

Add header values to the response body

  1. Create a JSON file that you use as the body for your request.

    cat << EOF > data.json
    {
      "payload": {
        "foo": "bar"
      }
    }
    EOF
  2. Create an EnterpriseKgatewayTrafficPolicy with the following transformation rules:

    • You extract the root and nested request headers by using a regular expression that captures the entire value of the header. You save the extracted values in the root and payload.nested extractors.
    • 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.
    • The MergeExtractorsToBody setting is used to automatically add all the extractors to the body.
    kubectl apply -f- <<EOF
    apiVersion: enterprisekgateway.solo.io/v1alpha1
    kind: EnterpriseKgatewayTrafficPolicy
    metadata:
      name: transformation
      namespace: httpbin
    spec:
      targetRefs:
      - group: gateway.networking.k8s.io
        kind: HTTPRoute
        name: httpbin
      entTransformation:
        stages:
          early:
            requests:
            - transformation:
                template:
                  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 the header. 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: '.*'
                  # Merge all extractors to the body.
                  bodyTransformation:
                    type: MergeExtractorsToBody
    EOF
  3. 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.

    curl -X POST -H "host: www.example.com:8080" \
    -H "Content-Type: application/json" \
    -H "root: root-val" \
    -H "nested: nested-val" http://$INGRESS_GW_ADDRESS:8080/post -d @data.json | jq
    curl -X POST localhost:8080/post \
    -H "host: www.example.com" \
    -H "Content-Type: application/json" \
    -H "root: root-val" \
    -H "nested: nested-val" -d @data.json | jq

    Example output:

    ...
    "json": {
     "payload": {
       "foo": "bar",
       "nested": "nested-val"
     },
     "root": "root-val"
    }
    

Cleanup

You can remove the resources that you created in this guide.

kubectl delete EnterpriseKgatewayTrafficPolicy transformation -n httpbin