Skip to content
You are viewing the latest documentation for Solo Enterprise for kgateway, formerly known as Gloo Gateway. To access the documentation for older Gloo Gateway versions, such as 2.0 and 1.x, use the version switcher.

Update response body

Page as Markdown

Learn how to return a customized response body and how replace specific values in the body.

In this guide, you use the following methods to transform a JSON body:

  • Directly access fields in the JSON body and inject them into a custom JSON body.
  • Use extractors to capture values in a JSON body and use the SingleReplace extractor mode to replace them with a specific text.
  • Use the replace_with_random Inja function to replace specific patterns in the JSON body.

Before you begin

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

  1. Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.
  1. 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

Update response body

  1. Send a request to the json endpoint of the httpbin app. The request returns a JSON body that you later transform.

    curl -vik http://$INGRESS_GW_ADDRESS:8080/json \
     -H "host: www.example.com:8080" 
    curl -vik localhost:8080/json \
    -H "host: www.example.com" 

    Example output:

    {
      "slideshow": {
        "author": "Yours Truly",
        "date": "date of publication",
       "slides": [
          {
            "title": "Wake up to WonderWidgets!",
            "type": "all"
          },
          {
            "items": [
              "Why <em>WonderWidgets</em> are great",
              "Who <em>buys</em> WonderWidgets"
            ],
            "title": "Overview",
            "type": "all"
          }
        ],
        "title": "Sample Slide Show"
      }
    }
  2. Create an EnterpriseKgatewayTrafficPolicy resource that transforms the JSON body as follows:

    • A new body is created in the response with the values of the author, title, and slides fields.
    • To extract the values, you use dot notation. Because the response is parsed as a JSON file, no extractors need to be defined. Instead, you can access the fields directly.
    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:
            responses:
            - transformation:
                template:
                  bodyTransformation: 
                    type: Body
                    body: '{"author": "{{ slideshow.author }}", "title": "{{ slideshow.title }}", "slides": "{{ slideshow.slides }}}'
    EOF
  3. Send another request to the json endpoint of the httpbin app. Verify that you see the transformed response body.

    curl -vik http://$INGRESS_GW_ADDRESS:8080/json \
     -H "host: www.example.com:8080" 
    curl -vik localhost:8080/json \
    -H "host: www.example.com" 

    Example output:

    {"author": "Yours Truly", "title": "Sample Slide Show", "slides": 
    "[{"title":"Wake up to WonderWidgets!","type":"all"},{"items":
    ["Why <em>WonderWidgets</em> are great","Who <em>buys</em> WonderWidgets"],
    "title":"Overview","type":"all"}]}
    
  4. Update the EnterpriseKgatewayTrafficPolicy resource to replace the word author with the word editor. The rest of the original response body is kept.

    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:
            responses:
            - transformation:
                template:
                  extractors:
                    replaceAuthor:
                      body: true
                      mode: SingleReplace
                      replacementText: 'editor'
                      regex: '[\s\S]*(author)[\s\S]*'
                      subgroup: 1
                  bodyTransformation:
                    type: Body
                    body: '{{ replaceAuthor }}'
    EOF
  5. Send another request to the httpbin app. Verify that the author field is replaced with editor and that the rest of the response body is not changed.

    curl -vik http://$INGRESS_GW_ADDRESS:8080/json \
     -H "host: www.example.com:8080" 
    curl -vik localhost:8080/json \
    -H "host: www.example.com" 
    Example output:

    {
      "slideshow": {
        "editor": "Yours Truly",
        "date": "date of publication",
        "slides": [
          {
            "title": "Wake up to WonderWidgets!",
            "type": "all"
          },
          {
            "items": [
              "Why <em>WonderWidgets</em> are great",
              "Who <em>buys</em> WonderWidgets"
            ],
            "title": "Overview",
            "type": "all"
          }
        ],
        "title": "Sample Slide Show"
      }
    }
    
  6. Update the EnterpriseKgatewayTrafficPolicy resource to replace the all pattern with a random string.

    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:
            responses:
            - transformation:
                template:
                  bodyTransformation:
                    type: Body
                    body: '{{ replace_with_random(body(), "all") }}'
    EOF
  7. Send another request to the httpbin app. Verify that every all value is replaced with a random string.

    curl -vik http://$INGRESS_GW_ADDRESS:8080/json \
     -H "host: www.example.com:8080" 
    curl -vik localhost:8080/json \
    -H "host: www.example.com" 
    Example output:
    {
      "slideshow": {
        "editor": "Yours Truly",
        "date": "date of publication",
        "slides": [
          {
            "title": "Wake up to WonderWidgets!",
            "type": "GGza0iF/51CsVwxovaiL1g"
          },
          {
            "items": [
              "Why <em>WonderWidgets</em> are great",
              "Who <em>buys</em> WonderWidgets"
            ],
            "title": "Overview",
            "type": "GGza0iF/51CsVwxovaiL1g"
          }
        ],
        "title": "Sample Slide Show"
      }
    }

Cleanup

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

kubectl delete EnterpriseKgatewayTrafficPolicy transformation -n httpbin