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.

Before you begin

  1. Follow the Get started guide to install Gloo Gateway, set up a gateway resource, and deploy the httpbin sample app.

  2. Get the external address of the gateway and save it in an environment variable.

Change the response status

  1. Create a payload that you later send as your request body.

      cat << EOF > data.json
    {
      "error": {
       "message": "This is an error"
      }
    }
    EOF
      
  2. 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.
  3. Send a request to the httpbin app and include data.json file as your payload. Because the error.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
       

  4. Update your payload file and remove the message so that the error field is empty.

      cat << EOF > data.json
    {
      "error": {
      }
    }
    EOF
      
  5. 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