Using the Request Body Transform middleware with Tyk OAS APIs
Last updated: 4 minutes read.
The request body transform middleware provides a way to modify the payload of API requests before they are proxied to the upstream.
The middleware is configured in the Tyk OAS API Definition. You can do this via the Tyk Dashboard API or in the API Designer.
If you’re using the legacy Tyk Classic APIs, then check out the Tyk Classic page.
Configuring the middleware in the Tyk OAS API Definition
The design of the Tyk OAS API Definition takes advantage of the operationId
defined in the OpenAPI Document that declares both the path and method for which the middleware should be added. Endpoint paths
entries (and the associated operationId
) can contain wildcards in the form of any string bracketed by curly braces, for example /status/{code}
. These wildcards are so they are human readable and do not translate to variable names. Under the hood, a wildcard translates to the “match everything” regex of: (.*)
.
The request body transformation middleware (transformRequestBody
) can be added to the operations
section of the Tyk OAS Extension (x-tyk-api-gateway
) in your Tyk OAS API Definition for the appropriate operationId
(as configured in the paths
section of your OpenAPI Document).
The transformRequestBody
object has the following configuration:
enabled
: enable the middleware for the endpointformat
: the format of input data the parser should expect (eitherxml
orjson
)body
: [see note] this is abase64
encoded representation of your templatepath
: [see note] this is the path to the text file containing the template
Note
You should configure only one of body
or path
to indicate whether you are embedding the template within the middleware or storing it in a text file. The middleware will automatically select the correct source based on which of these fields you complete. If both are provided, then body
will take precedence and path
will be ignored.
For example:
|
|
In this example the request body transform middleware has been configured for requests to the PUT /anything
endpoint. The body
contains a base64 encoded Go template (which you can check by pasting the value into a service such as base64decode.org).
Decoded, this template is:
{
"value1": "{{.value2}}",
"value2": "{{.value1}}",
"req-header": "{{._tyk_context.headers_X_Header}}",
"req-param": "{{._tyk_context.request_data.param}}"
}
So if you make a request to PUT /anything?param=foo
as follows:
PUT /anything?param=foo
HTTP/1.1
Host: my-gateway.host
X-Header: bar
{
"value1": "world",
"value2": "hello"
}
You will receive a response from the upstream with this payload:
{
"req-header": "bar",
"req-param": "[foo]",
"value1": "hello",
"value2": "world"
}
The /anything
endpoint returns the details of the request that was received by httpbin.org. You can see that Tyk has swapped value1
and value2
and embedded the X-Header
header and param
query values into the body of the request.
The configuration above is a complete and valid Tyk OAS API Definition that you can import into Tyk to try out the mock response middleware.
Note
If using a template in a file (i.e. you configure path
in the transformRequestBody
object), remember that Tyk will load and evaluate the template 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
Adding Request Body Transformation to your API endpoints is easy when using the API Designer in the Tyk Dashboard, simply follow the following steps:
Step 1: Add an endpoint
From the API Designer add an endpoint that matches the path and method to which you want to apply the middleware.
Step 2: Select the Request Body Transform middleware
Select ADD MIDDLEWARE and choose the Request Body Transform middleware from the Add Middleware screen.
Step 3: Configure the middleware
Now you can select the request body format (JSON or XML) and add either a path to the file containing the template, or directly enter the transformation template in the text box.
The Test with data control will allow you to test your body transformation function by providing an example request body and generating the output from the transform. It is not possible to configure headers, other request parameters, context or session metadata to this template test so if you are using these data sources in your transform it will not provide a complete output, for example:
Step 4: Save the API
Select SAVE API to apply the changes to your API.