Router does not work with number parameter in %CSP.REST
Hello,
I have a main dispatcher (that extends%CSP.REST) that contains:
<Routes>
<Map Prefix="/v([0-9]{1,2})/news" Forward="API.Dispatch.news"/>
</Routes>
And I have the sub-dispatcher that contains:
<Routes>
<Route Url="/:id" Method="GET" Call="NewsGetItem"/>
</Routes>
If I try to access this route:
https://mydomain.com/mobile/api/v4/news/123
It doesn't work (404).
But strangely enough, it works (200):
https://mydomain.com/mobile/api/v4/news/test
The ClassMethid is OK:
ClassMethod NewsGetItem(version As %Integer, id As %Integer) As %Status
{
w id
Quit $$$OK
}
As if the parameter :id did not work with a number.
Does anyone have any ideas or explanations?
Comments
Your issue seems related to the handling of numeric route parameters in the URL in a %CSP.REST dispatcher setup.
According to the documentation:
When you define route parameters in a URL by prefixing them with a colon (
:), these parameters are passed to the corresponding ObjectScript method. The issue might be with the parameter data type in your method definition. Numeric route parameters can sometimes cause unexpected behavior if the parameter types do not align as expected in the method signature. Ensure the route method declaration matches the expected type or uses%Stringfor flexibility [1][1].It's also recommended that names of REST route arguments in the URL be consistent in order and match the method arguments for proper mapping [3].
For example, your sub-dispatcher has the route <Route Url="/:id" Method="GET" Call="NewsGetItem"/>. Ensure the NewsGetItem method signature correctly handles the id parameter, such as:
ClassMethod NewsGetItem(version As %Integer, id As %String) As %Status
{
Write id
Quit $$$OK
}
This declares id as a %String, ensuring compatibility with URL parameters irrespective of their values [1][1][3].
If issues persist, consider debugging as per REST documentation and testing multiple parameter scenarios [1][1][3].
Sources:
try to change:
<Route Url="/{id:[0-9]+}" Method="GET" Call="NewsGetItem"/>
Hello,
Already tried and same result :(
Your NewsGetItem method has two parameters, version and id, but the route you have defined for it only has one parameter, id. The parameter lists need to match up and be in the same order. Try taking the version parameter our of the NewsGetItem class method.
I tried:
In main dispatcher:
<Routes>
<Map Prefix="/v1/news" Forward="API.Dispatch.news"/>
</Routes>
And in the sub-dispatcher:
<Routes>
<Route Url="/:id" Method="GET" Call="NewsGetItem"/>
</Routes>
And so with classmethod:
ClassMethod NewsGetItem(id As %Integer) As %Status
{
w id
Quit $$$OK
}
But still 404
Try with:
<Map Prefix="/v([0-9]{1,2})/news/:id" Forward="API.Dispatch.news"/>
Yes it work but I want to keep a main dispatcher with only the base route:
<Map Prefix="/v([0-9]{1,2})/news" Forward="API.Dispatch.news"/>
Try with:
<Map Prefix="/v([0-9]{1,2})/news/([^/]*)" Forward="API.Dispatch.news"/>