RejectedSoftware Forums

Sign up

Optional queryParam for REST GETs

Hi... I'm trying to figure out how to set up a REST API with optional
queryParams. Something along the lines of:

interface API
{

 @queryParam("p1", "p1")
 @path("/api/obj")
 Json querybyp1(string p1);

 @queryParam("p2", "p2")
 @path("/api/obj")
 Json querybyp2(string p2);

}

And have the URLs: "http://127.0.0.1/api/obj?p1=abc" and
"http://127.0.0.1/api/obj?p2=def" go to the two different functions.
One or the other must be supplied. For further consideration, what gets
called if both are supplied....

Re: Optional queryParam for REST GETs

On Thu, 16 Jul 2015 11:20:23 -0700, Andre Kostur wrote:

Hi... I'm trying to figure out how to set up a REST API with optional
queryParams. Something along the lines of:

interface API
{

 @queryParam("p1", "p1")
 @path("/api/obj")
 Json querybyp1(string p1);

 @queryParam("p2", "p2")
 @path("/api/obj")
 Json querybyp2(string p2);

}

And have the URLs: "http://127.0.0.1/api/obj?p1=abc" and
"http://127.0.0.1/api/obj?p2=def" go to the two different functions.
One or the other must be supplied. For further consideration, what gets
called if both are supplied....

Hum... Sadly, this isn't supported yet. And it won't be supported this way.

Currently, some improvements to the REST generators are blocked by the older compiler we support (2.065), which will be dropped as soon as the new release is out, which will happen when 2.068 gets released.

So when 2.068 is release, Vibe.d master will gain the ability to have optional parameters, through the use of Nullable. A limited implementation is already in a P.R. (for header params): https://github.com/rejectedsoftware/vibe.d/pull/1091

So your code would look like:

interface API
{
   @queryParam("p1", "p1")
   @path("/api/obj")
   Json querybyp1(Nullable!string p1, Nullable!string p2);
}

But you'll have to wait a bit for it to work (though I could push it to a P.R., if you really need it).
The reason it works this way is that the router only match on the path, not the query.