Inject response bodies
Verified Code examples on this page have been automatically tested and verified.Learn how to return a customized response body by using CEL expressions. The examples use request.path, request.method, request.headers[], request.body, json(), and string() to construct a dynamic response body string.
Before you begin
- Set up an agentgateway proxy.
- Install the httpbin sample app.
Inject request header fields into the response body
In this example, you set the response body to a JSON string built from request context variables. The gateway intercepts the upstream response and replaces the body with the CEL expression result before returning it to the client. The upstream never sees the change — only the client receives the modified body.
Create an AgentgatewayPolicy resource that sets the response body to a JSON object containing the request path, method, and
x-request-idheader value.kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayPolicy metadata: name: transformation namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin traffic: transformation: response: body: '"{\"path\": \"" + request.path + "\", \"method\": \"" + request.method + "\", \"request-id\": \"" + request.headers["x-request-id"] + "\"}"' EOFSend a request to the httpbin app and include an
x-request-idrequest header. Verify that you get back a 200 HTTP response code and that the response body contains the transformed output with the request header value.curl -vi http://$INGRESS_GW_ADDRESS:80/get \ -H "host: www.example.com:80" \ -H "x-request-id: user123"curl -vi localhost:8080/get \ -H "host: www.example.com" \ -H "x-request-id: user123"Example output:
< HTTP/1.1 200 OK HTTP/1.1 200 OK < content-type: application/json content-type: application/json < content-length: 49 content-length: 49 {"path": "/get", "method": "GET", "request-id": "user123"}
Inject request body fields into a response body
In this example, you parse a JSON request body by using the json() function to extract a field and include it in the response body. Use request.body to access the raw incoming request body as a string.
Create an AgentgatewayPolicy resource that reads the
namefield from the JSON request body and echoes it back in the response.kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayPolicy metadata: name: transformation namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin traffic: transformation: response: body: '"{\"hello\": \"" + string(json(request.body).name) + "\"}"' EOFSend a POST request to the httpbin app with a JSON body. Verify that you get back a 200 HTTP response code and that the response body contains the
namevalue from your request.curl -vi http://$INGRESS_GW_ADDRESS:80/post \ -H "host: www.example.com:80" \ -H "content-type: application/json" \ -d '{"name": "user123"}'curl -vi localhost:8080/post \ -H "host: www.example.com" \ -H "content-type: application/json" \ -d '{"name": "user123"}'Example output:
< HTTP/1.1 200 OK HTTP/1.1 200 OK < content-type: application/json content-type: application/json < content-length: 17 content-length: 17 {"hello": "user123"}
Cleanup
You can remove the resources that you created in this guide.kubectl delete AgentgatewayPolicy transformation -n httpbin