The following example walks you through how to use extractors to extract request header values by using regular expressions. The captured header values are then added to the response body by using the mergeExtractorsToBody setting. You can use the extractors to also indicate where you want to place the request header values in the body.

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.

Add header values to the response body

  1. Create a JSON file that you use as the body for your request.

      cat << EOF > data.json
    {
      "payload": {
        "foo": "bar"
      }
    }
    EOF
      
  2. Create a RouteOption or VirtualHostOption resource with your transformation rules. In the following example, you extract the root and nested request headers and add them to the response body by using the mergeExtractorsToBody setting. The dot notation that you use for the extractor names determines the placement of the header in the body. For example, if no dot notation is used, such as in root, the header is added to the body’s root level. If dot notation is used, such as in payload.nested, the extractor is added under the payload.nested field.

  3. Send a request to the httpbin app and include the root and nested request headers. Verify that you get back a 200 HTTP response code and that the value of the root header is added to the body’s root level, and that the nested header value is added under the payload.nested field in your response.

    Example output:

       ...
       "json": {
        "payload": {
          "foo": "bar",
          "nested": "nested-val"
        },
        "root": "root-val"
       }
       

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