0

I'm running Play! 2.2 with scala. I am trying to serve a JSON response. The following code will serve a response as application/json, however I want it to serve text/json.

I found some documentation here: http://www.playframework.com/documentation/2.1.x/ScalaJsonRequests However, the examples shown return application/json.

Here is an example of my function in the controller:

  def myContollerFunction = Action(parse.json) { request =>
     (request.body \ "foo").asOpt[String].map { foo =>
        Ok(Json.toJson(Map("foo" -> foo)))
     }}.getOrElse { 
       BadRequest("foo bar")
     }
   }

Here is an example output from cURL:

HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8

Hal
  • 69
  • 6

1 Answers1

2

You can add .as("text/json") to your result. (see play documentation)

Complete example:

def myContollerFunction = Action(parse.json) { request =>
   (request.body \ "foo").asOpt[String].map { foo =>
      Ok(Json.toJson(Map("foo" -> foo))).as("text/json")
   }}.getOrElse { 
      BadRequest("foo bar")
   }
}

That being said, it seems that application/json is the correct type for JSON data, see here.

Community
  • 1
  • 1
ValarDohaeris
  • 5,269
  • 5
  • 25
  • 41