Using the Request Body Transform middleware with Tyk Classic APIs
Last updated: 5 minutes read.
The request body transform middleware provides a way to modify the payload of API requests before they are proxied to the upstream.
This middleware is configured in the Tyk Classic API Definition at the endpoint level. You can do this via the Tyk Dashboard API or in the API Designer.
If you want to use dynamic data from context variables, you must enable context variables for the API to be able to access them from the request header transform middleware.
If you’re using the newer Tyk OAS APIs, then check out the Tyk OAS page.
If you’re using Tyk Operator then check out the Configuring the middleware in Tyk Operator section below.
Configuring the middleware in the Tyk Classic API Definition
To enable the middleware you must add a new transform
object to the extended_paths
section of your API definition.
The transform
object has the following configuration:
path
: the path to match onmethod
: this method to match ontemplate_data
: details of the Go template to be applied for the transformation of the request body
The Go template is described in the template_data
object by the following fields:
input_type
: the format of input data the parser should expect (eitherxml
orjson
)enable_session
: set this totrue
to make session metadata available to the transform templatetemplate_mode
: instructs the middleware to look for the template either in afile
or in a base64 encodedblob
; the actual file location (or base64 encoded template) is provided intemplate_source
template_source
: iftemplate_mode
is set tofile
, this will be the path to the text file containing the template; iftemplate_mode
is set toblob
, this will be abase64
encoded representation of your template
For example:
|
|
In this example, the Request Body Transform middleware is directed to use the template located in the file
at location ./templates/transform_test.tmpl
. The input (pre-transformation) request payload will be json
format and session metadata will be available for use in the transformation.
Note
Tyk will load and evaluate the template file when the Gateway starts up. If you modify the template, you will need to restart Tyk in order for the changes to take effect.
Configuring the middleware in the API Designer
You can use the API Designer in the Tyk Dashboard to configure the request body transform middleware for your Tyk Classic API by following these steps.
Step 1: Add an endpoint for the path and select the plugin
From the Endpoint Designer add an endpoint that matches the path for which you want to perform the transformation. Select the Body Transforms plugin.
Step 2: Configure the middleware
Ensure that you have selected the REQUEST
tab, then select your input type, and then add the template you would like to use to the Template input box.
Step 3: Test the Transform
If sample input data is available, you can use the Input box to add it, and then test it using the Test button. You will see the effect of the template on the sample input displayed in the Output box.
Step 4: Save the API
Use the save or create buttons to save the changes and activate the Request Body Transform middleware.
Configuring the middleware in Tyk Operator
The process for configuring a request body transform is similar to that defined in section Configuring the middleware in the Tyk Classic API Definition. Tyk Operator allows you to configure a request body transform by adding a transform
object to the extended_paths
section of your API definition.
In the example below the Request Body middleware (transform
) has been configured for HTTP POST
requests to the /anything
endpoint. The Request Body Transform middleware is directed to use the template located in the blob included in the template_source
field. The input (pre-transformation) request payload will be json format and session metadata will be available for use in the transformation.
|
|