I'm new to Scala, and this problem frustrates me. How can I get all headers from request?
val route =  {
  path("lol") {
    //get httpHeaders
    complete(HttpResponse())
  }
}
                You have at least two options here:
a) Using extractRequest directive:
val route = {
  path("example") {
    extractRequest { request =>
      request.headers // Returns `Seq[HttpHeader]`; do anything you want here
      complete(HttpResponse())
    }
  }
}
b) Explicitly accessing RequestContext:
val route =  {
  path("example") { ctx =>
    ctx.request.headers // Returns `Seq[HttpHeader]`; do anything you want here
    ctx.complete(...)
  }
}
There's also a whole family of directives related to headers, like headerValueByName or optionalHeaderValueByName. You can find the details here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With