使用 akka 路由 dsl 获取 http headers

Get http headers with akka routing dsl

我是 Scala 的新手,这个问题让我很沮丧。我如何从请求中获取所有 headers?

val route =  {
  path("lol") {
    //get httpHeaders
    complete(HttpResponse())
  }
}

您至少有两个选择:

a) 使用 extractRequest 指令:

val route = {
  path("example") {
    extractRequest { request =>
      request.headers // Returns `Seq[HttpHeader]`; do anything you want here
      complete(HttpResponse())
    }
  }
}

b) 显式访问 RequestContext:

val route =  {
  path("example") { ctx =>
    ctx.request.headers // Returns `Seq[HttpHeader]`; do anything you want here
    ctx.complete(...)
  }
}

还有一整套与 headers 相关的指令,例如 headerValueByNameoptionalHeaderValueByName。您可以找到详细信息 here.