1

I want to make a handler/controller for a GET request such as in ElasticSearch :

$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?routing=kimchy' -d '{
"query": {
    "filtered" : {
        "query" : {
            "query_string" : {
                "query" : "some query string here"
            }
        },
        "filter" : {
            "term" : { "user" : "kimchy" }
        }
    }
}}

I read the documentation from http://www.playframework.org/documentation/2.0.4/ScalaJsonRequests but the example is based on POST request. I've tried on my own it appears that I can access to the body request with POST request. But, when I try with the GET request, my request.body is AnyContentAsEmpty.

Is there a way to handle the json from this request in Play 2.0 ?


I saw that there is no body semantic for GET : Payloads of HTTP Request Methods . So maybe it's normal that there is no mechanism to deal with it through Play 2.0.

Community
  • 1
  • 1
alexgindre
  • 243
  • 1
  • 4
  • 11

2 Answers2

1

I believe you are confused on what can you expect on each type of request. To sum it up:

  1. GET requests contain the payload in the URL, no request body is added
  2. POST requests add the payload to the request body

From the example you post it seems that you want to return a Json answer as a result from a GET request, which would make more sense.

That can be easily achieved by crafting the Json string and using the Ok(result).as("application/json") to set the MIME type of the response.

Pere Villega
  • 16,379
  • 4
  • 59
  • 99
  • Thank you for your answer but I think I didn't well explain my problem. I don't want to create an handler that return json. I want to create an handler that can read json from a GET request. My example is the basic example from elasticsearch with curl. You perform a GET (-XGET from curl) request to the elasticsearch server with json. Then the elasticsearch server returns the json result of the search. Maybe I don't understand how works the curl cmd in this case. My guess is that -d force the request to use POST: _-d Sends the specified data in a POST request to the HTTP server_ – alexgindre Oct 26 '12 at 14:07
  • As I said in my post, in regards of this [link](http://stackoverflow.com/questions/978061/http-get-with-request-body) and this [link](http://stackoverflow.com/questions/5905916/payloads-of-http-request-methods) it seems possible to send request bodies within GET request. Therefor it's highly recommand to avoid such thing. Am I wrong ? – alexgindre Oct 26 '12 at 14:22
  • @alexgindre yes, GET uses the URL, POST the body. With -d you are doing a POST request, sending json by GET could be done by encoding it and adding it as a parameter, but I believe it's not a good option, better use POST. – Pere Villega Oct 26 '12 at 18:38
  • @yesterday thank you for your help! The curl cmd deceived me with -d param. Even it seems possible to use GET as I would, the playframework doesn't support this because it's too unorthodox, I guess. I will use POST or PUT. – alexgindre Oct 27 '12 at 16:23
0

this is a sample question

Play's default body parser follows the HTTP spec, and ignores the message body for GET, DELETE, HEAD and OPTIONS methods. If you want to force it to parse a body, you can do that by explicitly passing a body parser, eg:

 def delete = Action(parse.json) { implicit request =>
   val json = request.body
   val someProp = (json \ "someprop").as[String]
   Ok(s"Prop is: $someProp")
 }
PerkinsZhu
  • 11
  • 2