Sending HTTP request with multiple parameters having same name

50,549

Solution 1

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.

Solution 2

The standard http way of sending array data is

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

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

http://example.com/?data[]=1&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.

Solution 3

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");
Share:
50,549

Related videos on Youtube

n0rd
Author by

n0rd

Updated on July 20, 2020

Comments

  • n0rd
    n0rd almost 4 years

    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.

  • n0rd
    n0rd over 14 years
    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.
  • Anders Lindahl
    Anders Lindahl over 14 years
    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.
  • back2dos
    back2dos over 14 years
    +1, although actually i thought it'd be example.com/?data[]=1&data[]=2, whereas what you wrote is for sparse or associative arrays ... do you have any source, that states that all items but the last should be ignored? many servers act this way, but i couldn't find any specs actually demanding it ...
  • Tor Valamo
    Tor Valamo over 14 years
    I edited the sentence a bit. I don't know of any specs saying it, I added "in practice".
  • Tor Valamo
    Tor Valamo over 14 years
    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. ;)
  • n0rd
    n0rd over 14 years
    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.

Related