How to print content-type, headers, and body to debug a Play Framework controller

There are times when you’re debugging a Play Framework controller that you’ll want to print certain information, such as the request content-type, headers, content body, and query string. As a quick example, the code below shows how to print this information from a Play Framework controller method:

/**
 * The Sencha client will send me id, symbol, and companyName in a POST request.
 * I need to return something like this on success:
 *     { "success" : true, "msg" : "", "id" : 100 }
 */
def add = Action { implicit request =>

  // print the request debug information
  println(s"*** content-type: ${request.contentType}")
  println(s"*** headers: ${request.headers}")
  println(s"*** body: ${request.body}")
  println(s"*** query string: ${request.rawQueryString}")

  transactionForm.bindFromRequest.fold(
    errors => {
      Ok(Json.toJson(Map("success" -> toJson(false), "msg" -> toJson("Boom!"), "id" -> toJson(0))))
    },
    transaction => {
      println("*** CAME TO TRANSACTION > Fold > Transaction/Success ***")
      val id = Transaction.insert(transaction)
      id match {
        case Some(autoIncrementId) =>
            Ok(Json.toJson(Map("success" -> toJson(true), "msg" -> toJson("Success!"), "id" -> toJson(autoIncrementId))))
        case None =>
            Ok(Json.toJson(Map("success" -> toJson(true), "msg" -> toJson("Success!"), "id" -> toJson(-1))))
      }
      
    }
  )
}

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.