Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document optional query parameters #98

Closed
OndrejSpanel opened this issue Dec 16, 2023 · 2 comments
Closed

Document optional query parameters #98

OndrejSpanel opened this issue Dec 16, 2023 · 2 comments

Comments

@OndrejSpanel
Copy link
Contributor

OndrejSpanel commented Dec 16, 2023

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
Copy link
Member

lihaoyi commented Jan 4, 2024

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 lihaoyi closed this as completed Jan 4, 2024
@lihaoyi
Copy link
Member

lihaoyi commented Jan 4, 2024

Added more docs in #107

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants