views:

542

answers:

4

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.

A: 

You cannot use loadvars like this - because data can be either 1 or 2 or 3, not all of them at the same time.

You can either pass it as a comma separated list:

var req:LoadVars = new LoadVars();
req["data"] = "1,2,3";

or as an xml string, and parse it at the server. I am not familiar with manipulating xml in AS2, but this is how you'd do it in AS3:

var xml:XML = <root/>;
xml.appendChild(<data>1</data>);
xml.appendChild(<data>2</data>);
xml.appendChild(<data>3</data>);

//now pass it to loadvars
req["data"] = xml.toXMLString();

The string you send is:

<root>
  <data>1</data>
  <data>2</data>
  <data>3</data>
</root>
Amarghosh
I know I cannot do it with LoadVars, that's why I'm asking the question. I mean, LoadVars is obviously not the only method for sending HTTP requests. Unfortunately, neither concatenating parameters into single, nor sending request as XML is not appropriate.
n0rd
Again, my AS2 is limited - but whatever method you use to send data, **one variable can hold only one value** and hence you must do some encoding like concatenation or xml or json-arrays to accomplish this.
Amarghosh
I'm not asking about variables, I want to have multiple request parameters with same name, which are quite unrelated to variables, unless someone (which is the case with LoadVars) for reasons unknown decide to make one-to-one correspondence between them.
n0rd
When you post a request parameter to the server saying data=1 and data=2, what do you expect the server to infer from that?
Amarghosh
Server is already working and handles several requests parameters with same name as a list of values. I can't change anything about that: server is already working with different types of clients written in other languages and no one will patch all that just because actionscript doesn't allow such requests by standard means.
n0rd
Are you posting multiple values for the same variable through another type of client - say javascript?
Amarghosh
Other clients are written in C++ and Java.
n0rd
Other clients were written by other people, BTW. I don't know how they work.
n0rd
+1  A: 

Disclaimer; I've never used Actionscript and have no means for testing this.

Putting the same variable name with several values on the query string is the standard way of sending multi-value variables (for example form checkboxes) to web servers. If LoadVars is capable of sending multiple values then it seems plausible that the values should be stored in an array:

req["someParam1"] = ["foo","bar","bas"];

There also seems to be a decode function to LoadVars, what happens if you try to import the query string you want into the object?:

req.decode("someParam1=foo&someParam1=bar&someParam1=bas");
Anders Lindahl
Unfortunately, when you assign an array to request parameter it does something like array.join(",") (well, probably it calls it's toString() method) and assignes it to request parameter (i.e. there's onyl one parameter with comma-separated values). If you decode string, then you get a value of type string that is equal to last of params value (for you example it would be "bas"). Seems that LoadVars is unable to send such requests by design.
n0rd
Indeed it does, the internal representation must be a mapping from string to string. You'll have to either construct the query string using some other tool or decode the different values on the server.
Anders Lindahl
+1  A: 

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 LoadVars and XML ... technically there's also loadVariables, 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:

  1. a server to forward your calls. if you have access to the server, then you create a new endpoint for AS2 clients, which will decode the requests and pass them to the normal endpoint.
  2. use javascript. with flash.external::ExternalInterface you 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::URLLoader with dataFormat set to flash.net::URLLoaderDataFormat.TEXT, and then again encode parameters to a string, and send them ...

hope this helps ... ;)

greetz

back2dos

back2dos
+1  A: 

The standard http way of sending array data is

http://example.com/?data[0]=1&amp;data[1]=2

But this isn't wrong either (added from comment):

http://example.com/?data[]=1&amp;data[]=2

Sending more parameters with the same name like you're doing, in practice means that all but the last item should be ignored. This is because when reading variables, the server overwrites (in memory) any item that has the same name as that one, because renaming a variable isn't good practice and never was.

I don't know much AS (none :p) but you'd access it as a list or array or whatever data structures it has.

Tor Valamo
back2dos
I edited the sentence a bit. I don't know of any specs saying it, I added "in practice".
Tor Valamo
And about sparse arrays... it's not a given that the server should care that [] means 'append to array'. And if you're going to send two thousand items, then there are better ways. ;)
Tor Valamo
Once again: server is already written and working and I cannot change anything. It understands such requests fine. It does not overwrite anything in memory when reading parameters.
n0rd