Sending a JSON object from Flash to Play Framework 2.0

426 views
Skip to first unread message

viper...@gmail.com

unread,
Jan 23, 2013, 5:29:59 AM1/23/13
to play-fr...@googlegroups.com

I am building an application in Flash that exchanges messages between itself and the server in the form of JSON objects.

My AS3 code :

var person:Object = new Object();
person.firstname = "A";
person.lastname = "B";
var url:String = "http://localhost:9000/Ithaca";
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.POST;
var requestVars:URLVariables = new URLVariables();
requestVars.myObject = JSON.stringify(person);
request.data = requestVars;
var loader:URLLoader = new URLLoader();
loader.load(request);

The routes file:

POST    /Ithaca         controllers.Application.ithaca

The Application.scala file:

package controllers


import play.api.libs.json._
import play.api.mvc._
import org.codehaus.jackson.JsonNode        
import org.codehaus.jackson.node.ObjectNode 

object Application extends Controller 
{
def ithaca = Action(parse.json) { request =>
(request.body \ "firstname").asOpt[String].map { name =>
Ok("JSON received")
}.getOrElse {
BadRequest("Bad Request. Try again")
}
}}

But, upon running the Flash application, I am getting this error

Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL:http://localhost:9000/Ithaca

Can somebody guide me how to send POST requests to the Play Framework? (P.S : The AS3 code works fine when I was using a PHP script to parse the JSON earlier)

Pascal Voitot Dev

unread,
Jan 23, 2013, 5:59:17 AM1/23/13
to play-fr...@googlegroups.com
The question would be: what exactly does your flash send to Play? headers and content?...
There is no reason it shouldn't work.

Pascal


--
 
 

eric vantillard

unread,
Jan 23, 2013, 6:40:13 AM1/23/13
to play-fr...@googlegroups.com
can you add a log of the request body and send us the response ?

       Logger.info(request.body.toString())

Try also using curl :

curl --header "Content-type: application/json" --request POST --data '{"firstname": "A", "lastname" : "B"}' http://localhost:9000/Ithaca
Message has been deleted
Message has been deleted
Message has been deleted

viper...@gmail.com

unread,
Jan 24, 2013, 2:33:38 AM1/24/13
to play-fr...@googlegroups.com
I changed my Action definition in Application.scala to 

def ithaca = Action { request =>

Logger.info(request.body.toString())
  request.body.asJson.map { json =>
    (json \ "firstname").asOpt[String].map { firstname =>
      Ok("Hello " + firstname)
    }.getOrElse {
      BadRequest("Missing parameter [firstname]")
    }
  }.getOrElse {
    BadRequest("Expecting Json data")
  }
}


The log file 

2013-01-24 10:19:11,279 - [INFO] - from play in main 
Listening for HTTP on port 9000...

2013-01-24 12:47:33,267 - [INFO] - from play in play-akka.actor.default-dispatcher-3 
Application started (Dev)

2013-01-24 12:48:55,727 - [INFO] - from application in play-akka.actor.actions-dispatcher-2 
AnyContentAsFormUrlEncoded(Map(myObject -> List({"firstname":"A","lastname":"B"})))

So, this means, the JSON is being received by Play. But, Flash still keeps giving me that error. Any idea why?

And weirdly, when I use curl, it is showing that the data sent is an invalid JSON! I have no idea why. (I am using cURL for windows 64 bit)
Here's the output:

Could not resolve host : A,; Host not found
Could not resolve host : lastname; Host not found
[globbing] unmatched close brace/bracket at pos 2

And then it shows the entire HTML formatting tags and ends with

<h1> Bad Request </h1>

<p id="detail"> For request  ' POST /Ithaca '  [Invalid JSON] </p>

Pascal Voitot Dev

unread,
Jan 24, 2013, 2:54:22 AM1/24/13
to play-fr...@googlegroups.com
It seems that you don't send json but a kind of form...

AnyContentAsFormUrlEncoded(Map(myObject -> List({"firstname":"A","lastname":"B"})))

Are you sure you send application/json content type?

Pascal




--
 
 

Eric VANTILLARD

unread,
Jan 24, 2013, 4:43:06 AM1/24/13
to play-fr...@googlegroups.com
can you add the contentype before sending the request ?
using : request.contentType = "application/json"; or : var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json"); request.requestHeaders.push(hdr);


--
 
 

viper...@gmail.com

unread,
Jan 25, 2013, 1:43:04 AM1/25/13
to play-fr...@googlegroups.com
@Pascal ,  @Eric : U guys are right. It wasn't actually a JSON. It seems, that if I use any URLVariables in the AS3 code, by default the MIME changes to FormURLEncoded type. 

So, after adding the request.contentType="application/json" , I also removed the URLVariables and encoded the data to JSON from a simple object instead. And now my log file displays : 

2013-01-25 12:09:20,712 - [INFO] - from play in main 
Listening for HTTP on port 9000...

2013-01-25 12:09:56,988 - [INFO] - from play in play-akka.actor.default-dispatcher-3 
Application started (Dev)

2013-01-25 12:09:57,715 - [INFO] - from application in play-akka.actor.actions-dispatcher-1 
AnyContentAsJson([{"firstname":"A","lastname":"B"}])

Thank u so much!

But there is still that Flash problem of Unhandled IO Error. I am not receiving anything from either the Ok() or the BadRequest() methods. If I use the normal Flash Player instead of the debugger version. it just stays blank. Anything I could do to resolve this?

viper...@gmail.com

unread,
Jan 25, 2013, 3:15:24 AM1/25/13
to play-fr...@googlegroups.com
Update : I encoded the message in Ok() to JSON too and Flash isn't showing any errors. 

Ok(Json.toJson(map("message" -> "Json received")))   

Flash is running smoothly now.

Thanks a lot for the help !! 
Reply all
Reply to author
Forward
0 new messages