Specifying multiple parameters with the same name using HTTPService

Ruby on Rails controllers will automatically convert parameters to an array if they are in a specific format, for example:

http://foo.com?x[]=1&x[]=5&x[]=bar

      

This converts to the following array:

['1','5','bar']

      

Is there a way to do this with an ActionScript 3 HTTPService object using the request parameter? For example, it would be nice to do something like the following:

var s:HTTPService = new HTTPService();
s.request['x[]'] = 1;
s.request['x[]'] = 5;
s.request['x[]'] = 'bar';

      

However, this will simply overwrite each value, resulting in only the last value being sent. Does anyone have a better idea? I know I can just add stuff to the query string, but I would like to do it in the body of the POST.

0


source to share


4 answers


I was working on this same problem. Fortunatly, Flex supports this out of the box.

Just use Array for the field value:

var service:HTTPService = new HTTPService();
service.useProxy = true;
service.destination = "myservicet";
service.resultFormat = HTTPService.RESULT_FORMAT_XML;

var fields:Array = ["categories", "organisation"];
var params:Object = new Object();
params.q = "stackoverflow";
params.rows = 0;
params.facet = "true";
params["facet.field"] = fields;
service.send(params);

      

HTTPService converts these t0 parameters to url:



facet = true & q = stackoverflow & facet% 2Efield = categories & facet% 2Efield = organization & rows = 0

Hope this helps!

Added for clarity. When there is only 1 argument in the array, do not pass the fields as an array. For some reason flex won't post this to the http service

+2


source


I usually do something like this ...


var s:HTTPService = new HTTPService();
s.url = "http://foo.com";
s.method = "post";
// add listeners...
s.addEventListenser(ResultEvent.RESULT,function(event:ResultEvent){

    mx.controls.Alert.show(event.result.toString());
});

// send the data...
s.send({
    a: 1,
    b: 5,
    c: "bar"
});


      

which will result in HTTP Get / POST:



http://foo.com?a=1&b=5&c=bar

You can also create an associative array and pass it to the HTTPService submit method, which will look something like this:



var postdata:Object = {};

postdata["a"] = 1;
postdata["b"] = 5;
postdata["c"] = "bar";

// s is the HTTPService from above...
s.send(postdata);


      

+1


source


You mentioned that all POST parameters must have the same name. Elements with the same name overwrite each other in an associative array. However, I've looked at calendar cells before and all 31 cells are in the Date category.

What I've done:

var params:Object = new Object;
for (var i:uint=0; i<31; i++){
  params["Date"+(jj.toString())] = date[i];
}

HTTPService....etc.
HTTPService.send(params);

      

So, on the POST receiving side, this will be interpreted as Date0...Date31

.

Don't know if it was the way you wanted and the message was so long ago.

0


source


Think about it. Why don't you massage all the elements under the same name? However, this means that you are sending the array to the receiving end.

If you are using POST-ing, how will this link to the URL?

0


source







All Articles