Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get http headers with akka routing dsl

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())
  }
}
like image 873
npmrtsv Avatar asked May 23 '16 20:05

npmrtsv


1 Answers

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.

like image 143
Paweł Jurczenko Avatar answered Oct 24 '22 06:10

Paweł Jurczenko