Hi
I am using Tyk (v5.1.0) for a POC and I have difficulties using the rewrite_url functionality.
I would like that when sending a request to http://tky.gateway.localhost/test/cities Tyk validates the request body and forwards the request to http://nginx/service/test/v1/create_cities.
Unfortunately the request is rewritten to http://nginx/service/test/v1/cities.
I have defined the following OAS definition:
{
"info": {
"title": "cities webservice",
"version": "1.0.0"
},
"openapi": "3.0.3",
"security": [],
"components": {
"schemas": {
"city": {
"type": "object",
"properties": {
"name": {
"description": "name of the city",
"type": "string"
},
"country": {
"description": "country name",
"type": "string"
}
},
"required": [
"name",
"country"
]
}
}
},
"paths": {
"/cities": {
"put": {
"operationId": "addCities",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"cities": {
"type": "array",
"items" : {
"$ref": "#/components/schemas/city"
}
}
}
}
}
}
},
"responses": {
"200": {
"description": ""
},
"405":{
"description":"Invalid input"
}
}
}
}
},
"x-tyk-api-gateway": {
"info": {
"name": "cities rewrite url",
"state": {
"active": true,
"internal": false
}
},
"middleware": {
"operations": {
"addCities": {
"urlRewrite": {
"enabled": true,
"pattern": "/cities",
"rewriteTo": "/create_cities"
}
}
}
},
"server": {
"listenPath": {
"strip": true,
"value": "/test/"
}
},
"upstream": {
"url": "http://nginx/service/test/v1"
}
}
}
I have also tried with different regex like:
.*/cities
/(\\w+)/cities
/test/cities
Can you please help to understand what I am doing wrong?
I have set the env-variable TYK_LOGLEVEL to debug so that I can have the debug log messages but I do not see any details how the matches conditions are mached or not.
Does a possibility exist to debug the url_rewrite ?
For eample if I send a request to the gateway I could have a debug message like:
urlRewrite is enabled for operaionId `addCities`
check for urlRewrite rules...
urlRewrite pattern is set to /cities
url `http://tky.gateway.localhost/test/cities` do not match pattern `/cities`
url rewrite from `http://tky.gateway.localhost/test/cities` to `http://nginx/service/test/v1/cities`
I have also tried with the example from the documentation and I do not see the url rewrited:
If I understand correctly the url should be transfromed:
- from: http:///example-url-rewrite/json/hello
- to : http://httpbin.org/anything?value1=json&value2=hello
But as you can see in the log below, the transformation didn’t ocurred.
time="Apr 08 10:37:41" level=debug msg="Started proxy"
time="Apr 08 10:37:41" level=debug msg="Stripping proxy listen path: /example-url-rewrite/"
time="Apr 08 10:37:41" level=debug msg="Upstream path is: /json/hello"
time="Apr 08 10:37:41" level=debug msg=Started api_id=0da74b182cdc4a4b87b473c7d3119010 api_name=example-url-rewrite mw=ReverseProxy org_id= ts=1712572661632807690
time="Apr 08 10:37:41" level=debug msg="Upstream request URL: /json/hello" api_id=0da74b182cdc4a4b87b473c7d3119010 api_name=example-url-rewrite mw=ReverseProxy org_id=
time="Apr 08 10:37:41" level=debug msg="Outbound request URL: http://httpbin.org/json/hello" api_id=0da74b182cdc4a4b87b473c7d3119010 api_name=example-url-rewrite mw=ReverseProxy org_id=
time="Apr 08 10:37:41" level=debug msg=Finished api_id=0da74b182cdc4a4b87b473c7d3119010 api_name=example-url-rewrite mw=ReverseProxy ns=277279933 org_id=
time="Apr 08 10:37:41" level=debug msg="Upstream request took (ms): 277.390233"
time="Apr 08 10:37:41" level=debug msg="Done proxy"
Thank you in advance for your help !
Resources I have used by implementing the rewrite_url: