By Alvin Alexander. Last updated: November 11, 2024
As a brief note, if you need to debug a ZIO HTTP Request
value, I just created this function to return its values as a String
, and it appears to work properly:
// for debugging
def formatRequest(request: Request): String =
s"""
|Method: ${request.method}
|Path: ${request.path}
|Headers: ${request.headers.map(h => s"${h.headerName}: ${h.renderedValue}").mkString("\n ")}
|URL: ${request.url}
|Query Params: ${
val params: QueryParams = request.url.queryParams
if (params.isEmpty) "none"
else (for ((k, v) <- params.map) yield s"$k: $v").mkString(", ")
}
|Remote Address: ${request.remoteAddress.getOrElse("unknown")}
|""".stripMargin
Here’s an example of how to call that Request
-debugging code:
// use it
Method.GET / "hello" / string("name") -> handler { (name: String, request: Request) =>
println(formatRequest(request))
Response.json(Greeting(s"Hello, $name!").toJson)
}
And this is what some sample output looks like:
# sample call: curl "http://localhost:8080/hello/fred?a=foo&b=bar" # output: Method: GET Path: /hello/fred Headers: Host: localhost:8080 User-Agent: curl/8.4.0 Accept: */* content-length: 0 URL: /hello/fred?a=foo&b=bar Query Params: a: Chunk(foo), b: Chunk(bar) Remote Address: /0:0:0:0:0:0:0:1