Skip to content
Latest (currently 2026.10.2) has the newest features, bug fixes, and CVE patches of Solo Enterprise for agentgateway.

For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.

Inject response body

Verified : Automated tests passed for: End-to-end run: Every step ran in a fresh test environment and the results were checked.
Page as Markdown

Construct and return a dynamic response body from request context variables using CEL expressions.

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 EnterpriseAgentgatewayPolicy 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: enterpriseagentgateway.solo.io/v1alpha1
    kind: EnterpriseAgentgatewayPolicy
    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 EnterpriseAgentgatewayPolicy resource that reads the name field from the JSON request body and echoes it back in the response.

    kubectl apply -f- <<EOF
    apiVersion: enterpriseagentgateway.solo.io/v1alpha1
    kind: EnterpriseAgentgatewayPolicy
    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 EnterpriseAgentgatewayPolicy transformation -n httpbin