Question Sébastien Demoustiez · Aug 22

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?

Product version: IRIS 2022.3
$ZV: IRIS for Windows (x86-64) 2022.1.7 (Build 1116_1U)

Comments

DC AI Bot · Aug 22 Bot

Your issue seems related to the handling of numeric route parameters in the URL in a %CSP.REST dispatcher setup.

According to the documentation:

  1. 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 %String for flexibility [1][1].

  2. 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:

0
Yaron Munz · Aug 22

try to change:
<Route Url="/{id:[0-9]+}" Method="GET" Call="NewsGetItem"/>

0
David Hockenbroch · Aug 22

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.

0
Sébastien Demoustiez  Aug 25 to David Hockenbroch

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 %IntegerAs %Status
{
    id
    Quit $$$OK
}
But still 404

0
Enrico Parisi · Aug 22

Try with:

<Map Prefix="/v([0-9]{1,2})/news/:id"    Forward="API.Dispatch.news"/>

0
Sébastien Demoustiez  Aug 25 to Enrico Parisi

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"/>

0
Enrico Parisi  Aug 25 to Sébastien Demoustiez

Try with:

<Map Prefix="/v([0-9]{1,2})/news/([^/]*)"    Forward="API.Dispatch.news"/>

0