Routes with optional parameters in Suave
Asked Answered
B

1

10

I have a web service with a hello world endpoint like this:

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 which can handle routes like this: 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

Optional parameters should only be allowed in a query parameter string and not in the path.

Any idea how to achieve this with Suave?

Bowrah answered 11/4, 2016 at 11:30 Comment(0)
J
10

For handling query parameters, I would probably just use the request function, which gives you all the information about the original HTTP request. You can use that to check the 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" ]
Juba answered 11/4, 2016 at 12:55 Comment(1)
Cool, thanks. I wasn't sure if there was an easier built-in way of doing this with Suave or if I had to handle the optional parameter myself. Would be nice at some point to configure default values and optional parameters as part of the route registration in some way, but for now this does the job!Bowrah

© 2022 - 2024 — McMap. All rights reserved.