com-lihaoyi / cask

Cask: a Scala HTTP micro-framework. Cask makes it easy to set up a website, backend server, or REST API using Scala
https://com-lihaoyi.github.io/cask/
Other
525 stars 55 forks source link

Document optional query parameters #98

Closed OndrejSpanel closed 8 months ago

OndrejSpanel commented 8 months ago

This took me a while to find:

  @cask.get("/get")
  def hello(param: String = "Default") = {
    s"Hello $param"
  }

or:

  @cask.get("/get")
  def hello(param: Option[String] = None) = {
    s"Hello $param"
  }

This way the query parameter is optional. It may seem intuitive, but I have tried several different ways before trying this (e.g. just making param: Option[String]. or providing overloads of def hello).

lihaoyi commented 8 months ago

This is covered in the section on Variable Routes I think

You can bind variables to endpoints by declaring them as parameters: these are either taken from a path-segment matcher of the same name (e.g. postId above), or from query-parameters of the same name (e.g. param above). You can make param take a : String to match ?param=hello, an : Int for ?param=123 a Seq[T] (as above) for repeated params such as ?param=hello&param=world, or : Option[T] for cases where the ?param=hello is optional.

lihaoyi commented 8 months ago

Added more docs in https://github.com/com-lihaoyi/cask/pull/107