scalaplayframework-2.6

Play Framework adding headers to http request


I've been trying to get WS to make a request to a json api. So far I am not able to get utf-8 to render correctly, documented here: Play Framework WS losing unicode chars from external api

I'm now trying to add charset to the http headers of the request. But, I don't seem to be able to do this. If I run the following:

val request = ws.url("http://myserver.org")
request.withHttpHeaders("charset" -> "utf-8")
println("request headers: " request.headers)

request.get().map { response =>
  println("response headers: " + response.headers)
...

This will generate the following:

request headers: Map()
response headers: Map(Date -> Buffer(Tue, 07 Nov 2017 20:11:58 GMT), Server -> Buffer(Jetty(8.1.5.v20120716)), Content-Type -> Buffer(application/json), Cache-Control -> Buffer(private, must-revalidate, max-age=0), Content-Length -> Buffer(9822), X-Content-Type-Options -> Buffer(nosniff))

Can anyone diagnose what I'm doing wrong? why is the request header map empty? I've followed the conventions for according the 2.6 scala doc


Solution

  • It's quite simple.

    The method withHttpHeaders returns a new request, that you're just discarding.

    Try change your code like this:

    val request = ws.url("http://myserver.org").withHttpHeaders("charset" -> "utf-8")