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

Unable to (or do not know how) pass query parameters #97

Closed OndrejSpanel closed 8 months ago

OndrejSpanel commented 8 months ago

I am not able to process even simple requests with query parameters.

Assume request http://localhost:8080/?param=hi

Following does not work:

object MinimalApplication extends cask.MainRoutes {
  @cask.get("/")
  def hello(request: cask.Request) = {
    s"Hello"
  }
  initialize()
}

The error is:

Unknown argument: "param"

Arguments provided did not match expected signature:

hello request cask.Request

Not knowing how to fix that, I have tried:

object MinimalApplication extends cask.MainRoutes {
  @cask.get("/")
  def hello(request: cask.Request)(param: String) = {
    s"Hello"
  }
  initialize()
}

Now I get an error:

Unknown argument: "param"

Arguments provided did not match expected signature:

hello param String

I did not find any documentation about query params. I do not see any annotation like @cask.Cookie, maybe there is a way, but not documented?

lihaoyi commented 8 months ago

This is covered in the docs under Variable Routes

You need


object MinimalApplication extends cask.MainRoutes {
  @cask.get("/")
  def hello(param: String) = {
    s"Hello"
  }
  initialize()
}
OndrejSpanel commented 8 months ago

To provide a more complete example, when combined with request, the parameters are listed first:

object MinimalApplication extends cask.MainRoutes {
  @cask.get("/")
  def hello(param: String, request: cask.Request) = {
    s"Hello $param"
  }
  initialize()
}