Using the Request Validation middleware with Tyk Classic APIs
Last updated: 4 minutes read.
The request validation middleware provides a way to validate the presence, correctness and conformity of HTTP requests to make sure they meet the expected format required by the upstream API endpoints.
When working with legacy Tyk Classic APIs, request validation is performed by the Validate JSON
middleware which can be enabled per-endpoint. The schema against which requests are compared is defined in the middleware configuration and is limited to the request body (payload). Request headers and path/query parameters cannot be validated when using Tyk Classic APIs.
This middleware 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 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 validate_json
object to the extended_paths
section of your API definition.
The validate_json
object has the following configuration:
path
: the endpoint pathmethod
: the endpoint HTTP methodschema
: the JSON schema against which the request body will be comparederror_response_code
: the HTTP status code that will be returned if validation fails (defaults to422 Unprocessable Entity
)
For example:
|
|
In this example the Validate JSON middleware has been configured for requests to the POST /register
endpoint. For any call made to this endpoint, Tyk will compare the request body with the schema and, if it does not match, the request will be rejected with the error code HTTP 422 Unprocessable Entity
.
Note
The Validate JSON middleware supports JSON Schema draft-04
. Using another version will return an unsupported schema error, unable to validate
error in the Tyk Gateway logs.
Configuring the middleware in the API Designer
You can use the API Designer in the Tyk Dashboard to configure the request validation 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 validate the request payload. Select the Validate JSON plugin.
Step 2: Configure the middleware
Once you have selected the request validation middleware for the endpoint, you can select an error code from the drop-down list (if you don’t want to use the default 422 Unprocessable Entity
) and enter your JSON schema in the editor.
Step 3: Save the API
Use the save or create buttons to save the changes and activate the middleware.
Configuring the middleware in Tyk Operator
The process for configuring the middleware in Tyk Operator is similar to that explained in configuring the middleware in the Tyk Classic API Definition. To configure the request validation middleware you must add a new validate_json
object to the extended_paths
section of your API definition, for example:
The example API Definition below configures an API to listen on path /httpbin
and forwards requests upstream to http://httpbin.org.
In this example, the Validate JSON middleware has been configured for requests to the GET /get
endpoint. For any call made to this endpoint, Tyk will compare the request body with the schema and, if it does not match, the request will be rejected with the error code HTTP 422 Unprocessable Entity
.
|
|