How to use the request-validator plugin to validate whether a custom header is equal to an integer?
how to use request validator plugin to validate whether a custom header is equal to an integer
How do I use the request-validator plugin to check that a custom header equals a specific integer?
Define a parameter_schema entry for the header in the request-validator plugin config, using a JSON Schema with minimum and maximum set to the required integer value. Apply the plugin to the route through the Admin API, then Kong rejects requests where the header doesn’t match.
Overview
Steps
-
Write the below JSON file called
header-equal-int.json. The following config requires that thex-h1header value is1. Replacex-h1and1with your actual values:{ "name": "request-validator", "config": { "version": "draft4", "parameter_schema": [ { "name": "x-h1", "in": "header", "required": true, "schema": "{\"type\": \"number\", \"minimum\": 1, \"maximum\": 1}", "style": "simple", "explode": false } ] } } -
Enable the request-validator plugin on your route with the JSON file above:
curl -X POST http://{KONG}:8001/routes/{ROUTE}/plugins \ -H "Content-Type: application/json" \ --data @header-equal-int.jsonYou could also patch the above JSON file to an existing request-validator plugin:
curl -X PATCH http://{KONG}:8001/plugins/<PLUGIN-ID> \ -H "Content-Type: application/json" \ --data @header-equal-int.json -
Testing:
❯ curl <KONG>:8000/{ROUTE} -H "x-h1:2" {"message":"request param doesn't conform to schema"}% ❯ curl <KONG>:8000/{ROUTE} -H "x-h1:0" {"message":"request param doesn't conform to schema"}% ❯ curl <KONG>:8000/{ROUTE} -H "x-h1:a" {"message":"request param doesn't conform to schema"} ❯ curl <KONG>:8000/{ROUTE} -H "x-h1:1" 200 responseConfirmed Kong will proxy the request to upstreams only when
x-h1equals1.