Change response status
Update the response status based on fields that are present in the request body.
In this guide, you learn how to use an if else
function to change the HTTP status that is returned in your response. To determine the response status, you use a JSON string that is provided in the request. If the JSON includes an error, you return a 400 HTTP response code. In all other cases, you return the response code of the :status
pseudo header.
This guide uses a subset of the supported transformation template attributes. To review all the attributes that you can set, see Templating language.
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.
Change the response status
Create a payload that you later send as your request body.
cat << EOF > data.json { "error": { "message": "This is an error" } } EOF
Create a RouteOption or VirtualHostOption resource with the following transformation rules:
- Extract the
message
field from the data.json file that is sent in the request. - If the error message is not empty, return a 400 HTTP response code in the
:status
pseudo header. - In all other cases, do not change the
:status
pseudo header.
- Extract the
Send a request to the httpbin app and include
data.json
file as your payload. Because theerror.message
field is not empty, a 400 HTTP response code is returned.Example output:
{ "args": {}, "headers": { "Accept": [ "*/*" ], "Content-Length": [ "49" ], "Content-Type": [ "application/json" ], "Host": [ "www.example.com:8080" ], "User-Agent": [ "curl/8.7.1" ], "X-Envoy-Expected-Rq-Timeout-Ms": [ "15000" ], "X-Forwarded-Proto": [ "http" ], "X-Request-Id": [ "e462eb36-0a36-4ffd-b37d-316737819532" ] }, "origin": "10.0.2.124:53583", "url": "http://www.example.com:8080/post", "data": "{ \"error\": { \"message\": \"This is an error\" }}", "files": null, "form": null, "json": { "error": { "message": "This is an error" } } } 400
Update your payload file and remove the message so that the
error
field is empty.cat << EOF > data.json { "error": { } } EOF
Send another request to the httpbin app. Verify that this time a 200 HTTP response code is returned.
Example output:
{ "args": {}, "headers": { "Accept": [ "*/*" ], "Content-Length": [ "49" ], "Content-Type": [ "application/json" ], "Host": [ "www.example.com:8080" ], "User-Agent": [ "curl/8.7.1" ], "X-Envoy-Expected-Rq-Timeout-Ms": [ "15000" ], "X-Forwarded-Proto": [ "http" ], "X-Request-Id": [ "e462eb36-0a36-4ffd-b37d-316737819532" ] }, "origin": "10.0.2.124:53583", "url": "http://www.example.com:8080/post", "data": "{ \"error\": { \"message\": \"This is an error\" }}", "files": null, "form": null, "json": { "error": { } } } 200
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