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 extractors to capture values in a JSON body and use the
SINGLE_REPLACEextractor mode to replace them with a specific text. - Use the
replace_with_randomInja function to replace specific patterns in the JSON body.
Before you begin
Follow the Get started guide to install Gloo Gateway, set up a gateway resource, and deploy the httpbin sample app.
Get the external address of the gateway and save it in an environment variable.
export INGRESS_GW_ADDRESS=$(kubectl get svc -n gloo-system gloo-proxy-http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}") echo $INGRESS_GW_ADDRESSkubectl port-forward deployment/gloo-proxy-http -n gloo-system 8080:8080
Update response body
Send a request to the
jsonendpoint 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" } }Create a RouteOption or VirtualHostOption resource that transforms the JSON body as follows:
- 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: gateway.solo.io/v1 kind: RouteOption metadata: name: transformation namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin options: transformations: responseTransformation: transformationTemplate: parseBodyBehavior: ParseAsJson advancedTemplates: false body: text: '{"author": "{{ slideshow.author }}", "title": "{{ slideshow.title }}", "slides": "{{ slideshow.slides }}}' EOFkubectl apply -n gloo-system -f- <<EOF apiVersion: gateway.solo.io/v1 kind: VirtualHostOption metadata: name: transformation namespace: gloo-system spec: options: transformations: responseTransformation: transformationTemplate: parseBodyBehavior: ParseAsJson advancedTemplates: false body: text: '{"author": "{{ slideshow.author }}", "title": "{{ slideshow.title }}", "slides": "{{ slideshow.slides}}}' targetRefs: - group: gateway.networking.k8s.io kind: Gateway name: http namespace: gloo-system EOF
- A new body is created in the response with the values of the
Send another request to the
jsonendpoint 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"}]}Update the RouteOption or VirtualHostOption resource to replace the word
authorwith the wordeditor. The rest of the original response body is kept.kubectl apply -f- <<EOF apiVersion: gateway.solo.io/v1 kind: RouteOption metadata: name: transformation namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin options: transformations: responseTransformation: transformationTemplate: extractors: replaceAuthor: body: {} mode: SINGLE_REPLACE replacementText: 'editor' regex: '[\s\S]*(author)[\s\S]*' subgroup: 1 body: text: '{{ replaceAuthor }}' EOFkubectl apply -n gloo-system -f- <<EOF apiVersion: gateway.solo.io/v1 kind: VirtualHostOption metadata: name: transformation namespace: gloo-system spec: options: transformations: responseTransformation: transformationTemplate: extractors: replaceAuthor: body: {} mode: SINGLE_REPLACE replacementText: 'editor' regex: '[\s\S]*(author)[\s\S]*' subgroup: 1 body: text: '{{ replaceAuthor }}' targetRefs: - group: gateway.networking.k8s.io kind: Gateway name: http namespace: gloo-system EOFSend another request to the httpbin app. Verify that the
authorfield is replaced witheditorand that the rest of the response body is not changed.Example output:curl -vik http://$INGRESS_GW_ADDRESS:8080/json \ -H "host: www.example.com:8080"curl -vik localhost:8080/json \ -H "host: www.example.com" \{ "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" } }Update the RouteOption or VirtualHostOption resource to replace the
allpattern with a random string.kubectl apply -f- <<EOF apiVersion: gateway.solo.io/v1 kind: RouteOption metadata: name: transformation namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin options: transformations: responseTransformation: transformationTemplate: body: text: '{{ replace_with_random(body(), "all") }}' EOFkubectl apply -n gloo-system -f- <<EOF apiVersion: gateway.solo.io/v1 kind: VirtualHostOption metadata: name: transformation namespace: gloo-system spec: options: transformations: responseTransformation: transformationTemplate: extractors: replaceAuthor: body: {} mode: SINGLE_REPLACE replacementText: 'editor' regex: '[\s\S]*(author)[\s\S]*' subgroup: 1 body: text: '{{ replace_with_random(body(), "all") }}' targetRefs: - group: gateway.networking.k8s.io kind: Gateway name: http namespace: gloo-system EOFSend another request to the httpbin app. Verify that every
allvalue is replaced with a random string.Example output:curl -vik http://$INGRESS_GW_ADDRESS:8080/json \ -H "host: www.example.com:8080"curl -vik localhost:8080/json \ -H "host: www.example.com" \{ "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 virtualhostoption transformation -n gloo-system
kubectl delete routeoption transformation -n httpbin