4

I was trying to extract User-agent header from Request-Header, I tried this :

 headerValue(extractUserAgent) { userAgent =>

}


def extractUserAgent: HttpHeader => Option[String] = {
    case h: `User-Agent` => Some(h.)
    case x         => None
 }

I am stuck at line Some(h.),I was thinking it might give some String values as User-Agent there, but not string values are inside of It.Help please!!

Aamir
  • 2,380
  • 3
  • 24
  • 54

3 Answers3

4

I finally got a solution

path("test") {
  get {
    headerValueByName("User-Agent") { header =>
      complete(s"""{ "User-Agent" : "${header}" }""")
    }
  }
}
Aamir
  • 2,380
  • 3
  • 24
  • 54
0

In your example h is given to match any existing object which is given within . So unless User-Agent is an object to match against it will not work. You can update your code as below:

val uaHeader = "User-Agent".toLowerCase
def extractUA: HttpHeader => Option[String] = {
  case HttpHeader(`uaHeader`,value) => Some(value)
  case _         => None
}
eisbehr
  • 12,243
  • 7
  • 38
  • 63
gichpich
  • 1
  • 2
-3

I think you should read the documentation, its properly written and u can find your solution there:

http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0/scala/http/common/http-model.html

Sayim Khan
  • 82
  • 10