Routes with advanced options in Suave

I have a web service with a global welcome endpoint, for example:

let app = choose [ GET >=> choose [ path "/hello" >=> OK "Hello World!" pathScan "/hello/%s" (fun name -> OK (sprintf "Hello World from %s" name)) ] NOT_FOUND "Not found" ] [<EntryPoint>] let main argv = startWebServer defaultConfig app 0 

Now I would like to add an additional endpoint that can handle such routes: http://localhost:8083/hello/{name}?lang={lang}

This route should work for the following URLs:

but it should not work for

http: // localhost: 8083 / hello / FooBar / en-GB

Additional parameters should be allowed only in the query parameter string, and not in the path.

Any idea how to achieve this with Suave?

+8
f # routing suave
source share
1 answer

To process the request parameters, I would simply use the request function, which gives you all the information about the original HTTP request. You can use this to check query parameters:

 let handleHello name = request (fun r -> let lang = match r.queryParam "lang" with | Choice1Of2 lang -> lang | _ -> "en-GB" OK (sprintf "Hello from %s in language %s" name lang)) let app = choose [ GET >=> choose [ path "/hello" >=> OK "Hello World!" pathScan "/hello/%s" handleHello ] NOT_FOUND "Not found" ] 
+9
source share

All Articles