Update response body
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 the
replace_with_randomInja function to replace specific patterns in the JSON body.
The steps in this section use the Envoy-based kgateway data plane. The steps do not work with the agentgateway data plane.
Solo Enterprise for agentgatewayBefore you begin
Follow the Get started guide to install Gloo Gateway.
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.
Update response body
Send a request to the
jsonendpoint of the httpbin app. The request returns a JSON body that you later transform.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" } }Create a GlooTrafficPolicy resource with your transformation rules:
- A new body is created in the response with the values of the
author,title, andslidesfields. - 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: gloo.solo.io/v1alpha1 kind: GlooTrafficPolicy metadata: name: transformation namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin transformation: response: body: parseAs: AsJson value: '{"author": "{{ slideshow.author }}", "title": "{{ slideshow.title }}", "slides": "{{ slideshow.slides }}}' EOF- A new body is created in the response with the values of the
Send a request to the
jsonendpoint of the httpbin app again. Verify that you see the transformed response body.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"}]}Update the GlooTrafficPolicy resource to replace the
allpattern with a random string.kubectl apply -f- <<EOF apiVersion: gloo.solo.io/v1alpha1 kind: GlooTrafficPolicy metadata: name: transformation namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin transformation: response: body: parseAs: AsJson value: '{{ replace_with_random(body(), "all") }}' EOFSend another request to the
jsonendpoint of the httpbin app. Verify that everyallvalue is replaced with a random string.Example output:
{ "slideshow": { "author": "Yours Truly", "date": "date of publication", "slides": [ { "title": "Wake up to WonderWidgets!", "type": "5pESMzYNtg8W9AG/eVZ13A" }, { "items": [ "Why <em>WonderWidgets</em> are great", "Who <em>buys</em> WonderWidgets" ], "title": "Overview", "type": "5pESMzYNtg8W9AG/eVZ13A" } ], "title": "Sample Slide Show" } }
Cleanup
You can remove the resources that you created in this guide.
kubectl delete GlooTrafficPolicy transformation -n httpbin