Chk0nDanger
Chk0nDanger

Reputation: 1231

playframework 2.4 - Unspecified value parameter headers error

I am upgrading playframework 2.4 from 2.3, I changed versions then if I compile same code, I see following error. Since I am novice at Scala, I am trying to learn Scala to solve this issue but still don't know what is the problem. What I want to do is adding a request header value from original request headers. Any help will be appreciated.

[error] /mnt/garner/project/app-service/app/com/company/playframework/filters/LoggingFilter.scala:26: not enough arguments for constructor Headers: (headers: Seq[(String, String)])play.api.mvc.Headers.
[error] Unspecified value parameter headers.
[error]     val newHeaders = new Headers { val data = (requestHeader.headers.toMap

The LoggingFilter class

class LoggingFilter extends Filter {

  val logger = AccessLogger.getInstance();

  def apply(next: (RequestHeader) => Future[Result])(requestHeader: RequestHeader): Future[Result] = {

    val startTime = System.currentTimeMillis

    val requestId = logger.createLog();

    val newHeaders = new Headers { val data = (requestHeader.headers.toMap
      + (AccessLogger.X_HEADER__REQUEST_ID -> Seq(requestId))).toList }
    val newRequestHeader = requestHeader.copy(headers = newHeaders)
    next(newRequestHeader).map { result =>
      val endTime = System.currentTimeMillis
      val requestTime = endTime - startTime

      val bytesToString: Enumeratee[ Array[Byte], String ] = Enumeratee.map[Array[Byte]]{ bytes => new String(bytes) }
      val consume: Iteratee[String,String] = Iteratee.consume[String]()
      val resultBody : Future[String] = result.body |>>> bytesToString &>> consume

      resultBody.map {
        body =>
          logger.finish(requestId, result.header.status, requestTime, body)
      }
      result;

    }
  }


}

Edit

I updated codes as following and it compiled well

following codes changed

    val newHeaders = new Headers { val data = (requestHeader.headers.toMap
      + (AccessLogger.X_HEADER__REQUEST_ID -> Seq(requestId))).toList }

to

    val newHeaders = new Headers((requestHeader.headers.toSimpleMap
      + (AccessLogger.X_HEADER__REQUEST_ID -> requestId)).toList)

Upvotes: 1

Views: 1403

Answers (1)

Markus
Markus

Reputation: 1303

It simply states that if you want to construct Headers you need to supply a field named headers which is of type Seq[(String, String)]. If you omit the inital new you will be using the apply function of the corresponding object for Headers which will just take a parameter of a vararg of (String, String) and your code should work. If you look at documentation https://www.playframework.com/documentation/2.4.x/api/scala/index.html#play.api.mvc.Headers and flip between the docs for object and class it should become clear.

Upvotes: 3

Related Questions