Wextux
Wextux

Reputation: 762

Get only the Header using the WS library from play framework 2.2.1. (Scala)

I am stuck using the 2.2.1 version of the play framework and I am trying to check the Content-Length of a request for a file using the WS libraries without actually downloading the body of the request.

In the version 2.3.x of play there has been a new method added getStream() that lets you process the request yourself with a stream, but I am stuck on version 2.2.1 for now and cannot find a similar way.

So far I have tried this but it never seems to get a response:

import play.api.libs.ws.{ResponseHeaders, WS}
import play.api.libs.iteratee.Concurrent.joined

WS.url(url).get {
  (rsp: ResponseHeaders) =>
     val (wsConsumer, _) = joined[Array[Byte]]
     val size = rsp.headers.get("Content-Length").map(_.head).fold(0L)(_.toLong)
     play.Logger.info(s"Size: $size")
     wsConsumer
}

Upvotes: 1

Views: 546

Answers (1)

Michael Zajac
Michael Zajac

Reputation: 55569

WSRequestHolder has a head method that will perform a HEAD request to the remote server. i.e. the response will contain no body.

WS.url(url).head().map { response =>
  val size = response.header("Content-Length").fold(0L)(_.toLong)
  play.Logger.info(s"Content-Length is $size")
}

Upvotes: 2

Related Questions