All,
I’m working on an Flash AS2 project that consumes and sends JSON.
I’m using http://json.org/json.as to receive and parse JSON – it works great.
Here’s the trouble – one of the requirements is that the app POST JSON to a web service.
In prior projects, I’ve used LoadVars.sendAndLoad() with fine results.
The problem is that, with loadVars, you’re sending name/value pairs. However, the particular service that I’m sending JSON to in this project requires that the JSON must be the body of the POST. That is, not a name/value pair.
Unless I’m mistaken, LoadVars won’t let you do this.
So, I tried using XML.sendAndLoad:
dataAsString = JSON.stringify(json);
var send_xml:XML = new XML(dataAsString);
var response_xml:XML = new XML()
response_xml.onData = function(rawResponseData) {
trace (rawResponseData);
}
send_xml.sendAndLoad([url], response_xml, "POST");
… since XML.sendAndLoad() sends its data in the POST body. (Yes, I know this is ugly – ‘new XML(jsonString)’ can’t result in a valid XML object. But… it works.)
However, the roadblock I run into here is that the data POSTed by Flash gets encoded – quotes are replaced with “"“. In some cases, this would be fine, but the service I’m sending the JSON to does not permit this.
So, I’m looking for any of the following solutions:
- A way to use LoadVars.sendAndLoad()
without sending the data as
name/value pairs (i.e., the data
should be the body of the post) - A way to prevent XML.sendAndLoad()
from encoding data HTML
entities (e.g.,") - Another way to POST data from Flash
to a URL
For what it’s worth, AS3 handles this beautifully. Here’s an example of how easy this should be:
import com.adobe.serialization.json.*;
// requires the as3corelib from https://github.com/mikechambers/as3corelib
var request:URLRequest = new URLRequest("[the service URL]");
request.method = URLRequestMethod.POST;
request.contentType = 'application/json';
var jsonData:Object = {[application-specific data]};
request.data = JSON.encode(jsonData);
var loader:URLLoader = new URLLoader();
loader.load(request);
However, the rest of the application depends on an enormous amount of legacy AS2 code, so unfortunately, AS3 is not an option.
Many thanks in advance for any advice or suggestions!
Have you considered using an intermediate script as a proxy? I mean a url that receives the name=value pairs, then posts the value to the main url and return the results?