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

  1. Set up an agentgateway proxy.
  2. 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.

  1. Create an AgentgatewayPolicy resource that sets the response body to a JSON object containing the request path, method, and x-request-id header 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"] + "\"}"'
    EOF
  2. Send a request to the httpbin app and include an x-request-id request 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.

  1. Create an AgentgatewayPolicy resource that reads the name field 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) + "\"}"'
    EOF
  2. Send 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 name value 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
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.