Query Parameters

Raw

Raw query parameters can be accessed through Request.current.queryParamsRaw.
This is a Map[String, Seq[String]] which you can use to extract query parameters.
The raw approach is useful for simple cases and dynamic query parameters.

Typed

For more type safety you can use the QueryStringRW typeclass.
Make a case class MyParams(..) derives QueryStringRW
and then use it like this: Request.current.queryParams[MyParams]


Create a file query_params.sc and paste this code into it:

    //> using scala "3.6.4"
//> using dep ba.sake::sharaf:0.9.2

import io.undertow.Undertow
import ba.sake.querson.QueryStringRW
import ba.sake.sharaf.*, routing.*


val routes = Routes:
  case GET -> Path("raw") =>
    val qp = Request.current.queryParamsRaw
    Response.withBody(s"params = ${qp}")

  case GET -> Path("typed") =>
    case class SearchParams(q: String, perPage: Int) derives QueryStringRW
    val qp = Request.current.queryParams[SearchParams]
    Response.withBody(s"params = ${qp}")

Undertow.builder
  .addHttpListener(8181, "localhost")
  .setHandler(SharafHandler(routes))
  .build
  .start()

println(s"Server started at http://localhost:8181")

Then run it like this:

scala-cli query_params.sc 

Now go to http://localhost:8181/raw?q=what&perPage=10 and you will get the raw query params map:

params = Map(perPage -> List(10), q -> List(what))

and if you go to http://localhost:8181/typed?q=what&perPage=10 you will get a type-safe, parsed query params object:

params = SearchParams(what,10)