Question
· 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)
Discussion (5)3
Log in or sign up to continue

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: