Using the URL Rewrite middleware with Tyk OAS APIs
Last updated: 7 minutes read.
Tyk’s URL rewriter uses the concepts of triggers and rules to determine if the request (target) URL should be modified. These can be combined in flexible ways to create sophisticated logic to direct requests made to a single endpoint to various upstream services (or other APIs internally exposed within Tyk).
URL rewrite triggers and rules are explained in detail here.
When working with Tyk OAS APIs the rules and triggers are configured in the Tyk OAS API Definition; this can be done manually within the .json
file or from the API Designer in the Tyk Dashboard.
If you’re using the legacy Tyk Classic APIs, then check out this page.
Configuring the URL rewriter 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 URl rewrite middleware 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).
Using the basic trigger
For the basic trigger, you only need to enable the middleware (set enabled:true
) and then configure the pattern
and the rewriteTo
(target) URL. 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 required by the basic trigger.
|
|
In this example the basic trigger has been configured to match the path for a request to the GET /json
endpoint against the regex /(\w+)/(\w+)
. This is looking for two word groups in the path (after the API listen path) which, if found, will store the first string in context variable $1
and the second in $2
. The request (target) URL will then be rewritten to anything?value1=$1&value2=$2
.
If you send a request to GET http://localhost:8181/example-url-rewrite/json/hello
|
|
The URL rewrite middleware will match the pattern:
/json/hello
-> /(\w+)/(\w+)
-> $1
will take the value json
and $2
will take the value hello
It will then rewrite the target URL to /anything?value1=json&value2=hello
and httpbin.org
will respond with:
|
|
The configuration above is a complete and valid Tyk OAS API Definition that you can import into Tyk to try out the URL rewrite middleware.
Using advanced triggers
You can add advanced triggers to your URL rewriter configuration by adding the triggers
element within the urlRewrite
middleware configuration for the operation.
The triggers
element is an array, with one entry per advanced trigger. For each of those triggers you configure:
condition
to set the logical condition to be applied to the rules (any
orall
)rules
a list of rules for the triggerrewriteTo
the address to which the (target) URL should be rewritten if the trigger fires
The rules are defined using this format:
{
"in": key_location,
"name": key_name,
"pattern": pattern,
"negate": true/false //set to true to trigger if pattern does not match
}
Key locations are encoded as follows:
header
- request header parameterquery
- query parameterpath
- path parameter (i.e. components of the path itself)sessionMetadata
- session metadatarequestBody
- request bodyrequestContext
- request context
For example:
|
|
In this example, the basic trigger is configured as before, but two advanced triggers have been added.
The first advanced trigger will fire if the request has this configuration:
- query parameter
numBytes
is provided with a numeric value, AND - header parameter
x-bytes
is not set totrue
(note thatnegate
is set totrue
in this rule)
Such a request will be redirected to /anything
passing two query parameters
value1
with the first string matched in the basic trigger (i.e.json
)query
with the value provided in thenumBytes
query parameter
The second advanced trigger will fire if the first doesn’t and if this condition is met:
- query parameter
numBytes
is provided with a numeric value
Such a request will be redirected to /bytes/{numBytes}
, which will return numBytes
random bytes from httpbin.org
.
If neither advanced trigger fires, then the basic trigger will redirect the request to /anything?value1=json&value2=hello
as before.
The configuration above is a complete and valid Tyk OAS API Definition that you can import into Tyk to try out the URL rewrite middleware.
Configuring the URL rewriter in the API Designer
Adding and configuring the URL rewrite feature to your API endpoints is easy when using the API Designer in the Tyk Dashboard, simply follow these 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 URL Rewrite middleware
Select ADD MIDDLEWARE and choose the URL Rewrite middleware from the Add Middleware screen.
Step 3: Configure the basic trigger
Add the match pattern and the new URL to configure the basic trigger rule.
Step 4: Optionally configure advanced triggers
You can optionally apply advanced triggers by selecting ADD TRIGGER for each trigger you wish to configure. For each advanced trigger you can add one or more rules, selecting ADD RULE to add the second, third, etc.
Step 5: Save the API
Select ADD MIDDLEWARE to save the middleware configuration. Remember to select SAVE API to apply the changes.