Reading Play Framework HTTP request headers (examples)

If you ever need to work with HTTP request headers in a Play Framework application, I hope the following examples will help. I was just looking at trying to access request headers like “User-Agent” and “Referer,” and ran a few tests.

Note: I put all of the Scala code that follows in Play Framework controller actions, then accessed the URL that was associated with those actions in the Play routes file, using the latest version of the Firefox browser on a MacOS system.

Directly accessing request header fields

After running the tests that I show below, this is how I finally ended up reading the request header fields I wanted:

val host: String              = request.host
val userAgent: Option[String] = request.headers.get("User-Agent")
val remoteAddress: String     = request.remoteAddress
val referer: Option[String]   = request.headers.get("Referer")

When I printed those out with a logger, these are the results I saw:

logger.debug(s"host:          $host")
logger.debug(s"userAgent:     $userAgent")
logger.debug(s"remoteAddress: $remoteAddress")
logger.debug(s"Referer:       $referer")

[debug] c.UserController - host:          localhost:9000
[debug] c.UserController - userAgent:     Some(Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:59.0) Gecko/20100101 Firefox/59.0)
[debug] c.UserController - remoteAddress: 0:0:0:0:0:0:0:1
Referer:       None

Looping through all of the request headers

In an earlier look at request headers with the Play Framework I put this code in a controller action:

val headers: Seq[(String, String)] = request.headers.headers
for (h <- headers) {
    logger.debug(s"h: $h")
}

It resulted in the following output:

[info] play.api.Play - Application started (Dev)
[debug] c.UserController - h: (Content-Length,122)
[debug] c.UserController - h: (Content-Type,application/x-www-form-urlencoded)
[debug] c.UserController - h: (Remote-Address,0:0:0:0:0:0:0:1:57044)
[debug] c.UserController - h: (Raw-Request-URI,/users/doLogin)
[debug] c.UserController - h: (Tls-Session-Info,[Session-1, SSL_NULL_WITH_NULL_NULL])
[debug] c.UserController - h: (Host,localhost:9000)
[debug] c.UserController - h: (User-Agent,Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:59.0) Gecko/20100101 Firefox/59.0)
[debug] c.UserController - h: (Accept,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8)
[debug] c.UserController - h: (Accept-Language,en-US,en;q=0.5)
[debug] c.UserController - h: (Accept-Encoding,gzip, deflate)
[debug] c.UserController - h: (Referer,http://localhost:9000/users/login)
[debug] c.UserController - h: (Cookie,PLAY_SESSION=eyJhbGciOiJIUzI1NiJ9... (a very long string here))
[debug] c.UserController - h: (DNT,1)
[debug] c.UserController - h: (Connection,keep-alive)
[debug] c.UserController - h: (Upgrade-Insecure-Requests,1)
[debug] c.UserController - h: (Timeout-Access,<function1>)

Looping through all of the request headers (version 2)

This is another way I printed all of the HTTP request headers in a Play Framework controller action:

val headers: Map[String, String] = request.headers.toSimpleMap
for ((k,v) <- headers) {
    logger.debug(s"key: $k, value: $v")
}

And its output:

[debug] c.UserController - key: DNT, value: 1
[debug] c.UserController - key: Host, value: localhost:9000
[debug] c.UserController - key: Accept, value: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
[debug] c.UserController - key: Cookie, value: PLAY_SESSION=eyJhbGciOiJIUzI1NiJ9... (a very long string here)
[debug] c.UserController - key: Connection, value: keep-alive
[debug] c.UserController - key: User-Agent, value: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:59.0) Gecko/20100101 Firefox/59.0
[debug] c.UserController - key: Remote-Address, value: 0:0:0:0:0:0:0:1:57211
[debug] c.UserController - key: Timeout-Access, value: <function1>
[debug] c.UserController - key: Accept-Encoding, value: gzip, deflate
[debug] c.UserController - key: Accept-Language, value: en-US,en;q=0.5
[debug] c.UserController - key: Raw-Request-URI, value: /users/login
[debug] c.UserController - key: Tls-Session-Info, value: [Session-1, SSL_NULL_WITH_NULL_NULL]
[debug] c.UserController - key: Upgrade-Insecure-Requests, value: 1

Summary

If you needed to see how to read HTTP request headers in a Play Framework application, I hope these examples are helpful.