Append headers
Use the headersToAppend capability to add multiple values to the same header.
In this guide, you explore how to use the headersToAppend transformation template attribute to add multiple values to the same header. The values are separated with a comma.
Before you begin
Follow the Get started guide to install Solo Enterprise for kgateway.
- Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.
- 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_ADDRESSkubectl port-forward deployment/http -n kgateway-system 8080:8080
Append headers
Create an EnterpriseKgatewayTrafficPolicy with the following transformation rules:
- Extract the value of the
Hostrequest header and use this value to build thecustomcookie2string. Then, append the transformed string to theSet-Cookierequest header. - Extract the value of the
Hostrequest header and use this value to build thecustomcookie3string. Then, append the transformed string to the sameSet-Cookierequest header.
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: headersToAppend: - key: Set-Cookie value: 'customcookie2={{ request_header("Host") }}' - key: Set-Cookie value: 'customcookie3={{ request_header("Host") }}' EOF- Extract the value of the
Send a request to the httpbin app. Verify that you see multiple values in the
Set-Cookierequest header.curl -vik -H "host: www.example.com:8080" \ -H "Content-Type: application/json" \ http://$INGRESS_GW_ADDRESS:8080/headerscurl -vik localhost:8080/headers \ -H "host: www.example.com" \ -H "Content-Type: application/json"Example output:
{ "headers": { "Accept": [ "*/*" ], "Host": [ "www.example.com:8080" ], "Set-Cookie": [ "customcookie2=www.example.com:8080", "customcookie3=www.example.com:8080" ], "User-Agent": [ "curl/8.7.1" ], "X-Envoy-Expected-Rq-Timeout-Ms": [ "15000" ], "X-Forwarded-Proto": [ "http" ], "X-Request-Id": [ "09fbe5ed-5060-4cf5-835c-46729bf100db" ] } }
Cleanup
You can remove the resources that you created in this guide.
kubectl delete EnterpriseKgatewayTrafficPolicy transformation -n httpbin