Freya - How to define a Uri pattern with optional query string parameters

I use the Freya web stack to create a REST API and am having trouble defining a URI template for the route.

I defined a Uri template that looks like this:

"/{store}/{partition}{?date}" 

The Freya router now matches this Uri pattern with the following URLs:

 http://localhost/a/1?date=2015 http://localhost/b/3?date=2013 

However, if I omit the query string, the Uri pattern no longer matches the URL. For example, the following URLs do not match the pattern:

 http://localhost/a/1 http://localhost/b/3 

How to define a Uri pattern using Freya with optional query string parameters?

Any help is greatly appreciated, thanks in advance.

+5
source share
1 answer

I could not find anything in the Freya source code, which suggests that query string parameters can be defined as optional in the Uri template.

However, I can define two routes that can be defined using two separate Uri patterns pointing to the same freyaMachine.

I ended up with two ways:

 let routes = freyaRouter { route (Methods [GET]) "/{store}/{partition}" myMachine route (Methods [GET]) "/{store}/{partition}{?date}" myMachine } |> FreyaRouter.FreyaPipeline 
+3
source

Source: https://habr.com/ru/post/1242992/


All Articles