I need to send a HTTP request (and get XML response) from Flash that looks similar to following:
http://example.com/somepath?data=1&data=2&data=3
I.e. having several parameters that share same name, but have different values.
Until now I used following code to make HTTP requests:
var resp:XML = new XML();
resp.onLoad = function(success:Boolean) {/*...*/};
resp.ignoreWhite = true;
var req:LoadVars = new LoadVars();
req["someParam1"] = 3;
req["someParam2"] = 12;
req.sendAndLoad("http://example.com/somepath", resp, "GET");
In this case this will not do: there will be only one parameter having last value.
What are my options? I’m using actionscript 2.
Added
I guess, I can do something like that:
var url:String = myCustomFunctionForBuildingRequestString();
var resp:XML = new XML();
resp.onLoad = function(success:Boolean) {/*...*/};
resp.load(url);
But in that case I am loosing ability to do POST requests. Any alternatives?
Changing request is not appropriate.
Although POST may be having multiple values for the same key, I’d be cautious using it, since some servers can’t even properly handle that, which is probably why this isn’t supported … if you convert “duplicate” parameters to a list, the whole thing might start to choke, if a parameter comes in only once, and suddendly you wind up having a string or something … but i guess you know what you’re doing …
I am sorry to say so, but what you want to do, is not possible in pure AS2 … the only 2 classes available for HTTP are
LoadVarsandXML… technically there’s alsoloadVariables, but it will simply copy properties from the passed object into the request, which doesn’t change your problem, since properties are unique …if you want to stick to AS2, you need an intermediary tier:
flash.external::ExternalInterfaceyou can call JavaScript code. You need to define a callback for when the operation is done, as well as a JavaScript function that you can call (there are other ways but this should suffice). Build the request string inside flash, pump it to JavaScript and let JavaScript send it to the server in a POST request and get the response back to flash through the callback.up to you to decide which one is more work …
side note: in AS3, you’d use
flash.net::URLLoaderwithdataFormatset toflash.net::URLLoaderDataFormat.TEXT, and then again encode parameters to a string, and send them.