Request Method
Last updated:
Overview
Tyk’s Request Method Transform middleware allows you to modify the HTTP method of incoming requests to an API endpoint prior to the request being proxied to the upstream service. You might use this to map POST
requests from clients to upstream services that support only PUT
and DELETE
operations, providing a modern interface to your users. It is a simple middleware that changes only the method and not the payload or headers. You can, however, combine this with the Request Header Transform and Request Body Tranform to apply more complex transformation to requests.
Use Cases
Simplifying API consumption
In cases where an upstream API requires different methods (e.g. PUT
or DELETE
) for different functionality but you want to wrap this in a single client-facing API, you can provide a simple interface offering a single method (e.g. POST
) and then use the method transform middleware to map requests to correct upstream method.
Enforcing API governance and standardization
You can use the transform middleware to ensure that all requests to a service are made using the same HTTP method, regardless of the original method used by the client. This can help maintain consistency across different client applications accessing the same upstream API.
Error Handling and Redirection
You can use the method transformation middleware to handle errors and redirect requests to different endpoints, such as changing a DELETE request to a GET request when a specific resource is no longer available, allowing for graceful error handling and redirection.
Testing and debugging
Request method transformation can be useful when testing or debugging API endpoints; temporarily changing the request method can help to identify issues or test specific functionalities.
Working
This is a very simple middleware that is assigned to an endpoint and configured with the HTTP method to which the request should be modified. The Request Method Transform middleware modifies the request method for the entire request flow, not just for the specific upstream request, so all subsequent middleware in the processing chain will use the new (transformed) method.
If you’re using Tyk OAS APIs, then you can find details and examples of how to configure the request method transform middleware here.
If you’re using Tyk Classic APIs, then you can find details and examples of how to configure the request method transform middleware here.
Using Tyk OAS
Tyk’s request method transform middleware is configured at the endpoint level, where it modifies the HTTP method used in the request to a configured value.
When working with Tyk OAS APIs the transformation 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.
API Definition
The request method transform middleware (transformRequestMethod
) 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). 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: (.*)
.
You only need to enable the middleware (set enabled:true
) and then configure toMethod
as the new HTTP method to which the request should be transformed. 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 method should be transformed.
All standard HTTP methods are supported: GET
, PUT
, POST
, PATCH
, DELETE
, HEAD
, OPTIONS
.
For example:
|
|
In this example the Request Method Transform middleware has been configured for requests to the GET /status/200
endpoint. Any request received to that endpoint will be modified to POST /status/200
.
The configuration above is a complete and valid Tyk OAS API Definition that you can import into Tyk to try out the request method transform.
API Designer
Adding the transform to your API endpoints is easy when using the API Designer in the Tyk Dashboard, simply follow these steps:
-
Add an endpoint
From the API Designer add an endpoint that matches the path and method to which you want to apply the middleware.
-
Select the Method Transform middleware
Select ADD MIDDLEWARE and choose the Method Transform middleware from the Add Middleware screen.
-
Configure the middleware
Select the new HTTP method to which requests to this endpoint should be transformed
Select ADD MIDDLEWARE to apply the change to the middleware configuration.
-
Save the API
Select SAVE API to apply the changes to your API.
Using Classic
Tyk’s request method transform middleware is configured at the endpoint level, where it modifies the HTTP method used in the request to a configured value.
When working with Tyk Classic APIs the transformation is configured in the Tyk Classic API Definition. You can do this via the Tyk Dashboard API or in the API Designer.
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 a Request Method Transform in Tyk Operator section below.
API Definition
To configure a transformation of the request method you must add a new method_transforms
object to the extended_paths
section of your API definition.
It has the following configuration:
path
: the endpoint pathmethod
: the endpoint HTTP methodto_method
: The new HTTP method to which the request should be transformed
All standard HTTP methods are supported: GET
, PUT
, POST
, PATCH
, DELETE
, HEAD
, OPTIONS
.
For example:
{
"method_transforms": [
{
"path": "/status/200",
"method": "GET",
"to_method": "POST"
}
]
}
In this example the Request Method Transform middleware has been configured for HTTP GET
requests to the /status/200
endpoint. Any request received to that endpoint will be modified to POST /status/200
.
API Designer
You can use the API Designer in the Tyk Dashboard to configure the request method transform middleware for your Tyk Classic API by following these steps.
-
Add an endpoint for the path and select the Method Transform plugin
From the Endpoint Designer add an endpoint that matches the path for which you want to perform the transformation. Select the Method Transform plugin.
-
Configure the transform
Then select the HTTP method to which you wish to transform the request.
-
Save the API
Use the save or create buttons to save the changes and activate the middleware.
Tyk Operator
The process for configuring a request method transform for an endpoint in Tyk Operator is similar to that defined in section configuring a Request Method Transform in the Tyk Classic API Definition.
To configure a transformation of the request method you must add a new method_transforms
object to the extended_paths
section of your API definition:
|
|
The example API Definition above configures an API to listen on path /transform
and forwards requests upstream to http://httpbin.org.
In this example the Request Method Transform middleware has been configured for HTTP GET
requests to the /anything
endpoint. Any request received to that endpoint will be modified to POST /anything
.