Adding attributes to HTTP requests with Kong Gateway

    Kong Gateway supports a architecture including a Request Transformer Plugin that can modify incoming requests before proxying them to your upstream service. This can all be accomplished using a no-code solution and managed with no downtime using Kong’s dynamic administrative capabilities.

    This guide will show you how to configure the Request Transformer plugin using the to modify incoming requests with a static constant value. Then you will test the feature with a mock request to verify the transformation process.

    • You have installed on your system, which is used to send requests to the gateway. Most systems come with pre-installed.
    • This guide uses the jq command line JSON processing tool. While this tool is not necessary to complete the tasks, it’s helpful for processing JSON responses from the gateway. If you do not have jq or do not wish to install it, you can modify the commands to remove jq processing.

    There are a large number of Kong plugins, many of which need to be custom installed prior to utilization. Kong ships prepackaged with a number of useful plugins including the Request Transformer you will use in this guide.

    The command output should be:

    1. true

    Now, assign a new instance of the Request Transformer plugin to the mock service by sending a POST request to the Admin API. In this command, the config.add.body value instructs the plugin to add a new field to the body of incoming requests before forwarding them to the service. In this example, we are instructing the plugin to add a field named new-field and give it a static value of defaultValue.

    If successful the API will return a 201 Created HTTP response code with a JSON body including information about the new plugin instance.

    Next, use the mock service’s /requests endpoint to test the behavior of the plugin. The /requests API will echo back helpful information from the request we send it, including headers and the request body.

    1. -H 'Content-Type: application/json' \
    2. -d '{"existing-field": "abc123"}'

    The JSON response will contain the postData field which includes the JSON body sent to the service. You can use jq to fully extract the request body returned from the mock service, as follows:

    This will output the following text indicating new-field has been added to the request body.

    • If no standard plugin is available to satisfy your use case, the Plugin Development Guide can help you with developing your own plugin.