npm*_*tsv 9 scala akka akka-http
我是Scala的新手,这个问题让我感到沮丧.如何从请求中获取所有标头?
val route = {
path("lol") {
//get httpHeaders
complete(HttpResponse())
}
}
Run Code Online (Sandbox Code Playgroud)
Paw*_*nko 20
这里至少有两个选项:
a)使用extractRequest
指令:
val route = {
path("example") {
extractRequest { request =>
request.headers // Returns `Seq[HttpHeader]`; do anything you want here
complete(HttpResponse())
}
}
}
Run Code Online (Sandbox Code Playgroud)
b)明确访问RequestContext
:
val route = {
path("example") { ctx =>
ctx.request.headers // Returns `Seq[HttpHeader]`; do anything you want here
ctx.complete(...)
}
}
Run Code Online (Sandbox Code Playgroud)
还有一整套与标题相关的指令,如headerValueByName
或optionalHeaderValueByName
.你可以在这里找到详细信息.