Search code examples
iosiphoneasihttprequest

ASIHTTPRequest Multiple POST parameters


I'm using ASIFormDataRequest to post data to a server, and I need to post an array of values (strings in the example below). I'm using an enumerator to loop through each and add them to the request, but only the last one comes through - the behaviour you'd expect for setPostValue, not addPostValue.

Any idea why only one value is coming through?

NSEnumerator *pupilEnumerator = [pupils objectEnumerator];
id pupil;
while ((pupil = [pupilEnumerator nextObject])) {
  [request addPostValue:pupil forKey:@"pupils"];
}

Note this also occurs for addFile and addPostData.

Cheers.


Solution

  • If I were you I'd bundle up your pupils object into a JSON string (I'd use JSON Framework, but there are other options), and put that in the post body.

    Or, if you're posting to a PHP script, you CAN post multiple values straight into an array, by putting "[]" after the key name. So, still inside your loop, you could say [request addPostValue:pupil forKey:@"pupils[]"];, and then your posted-to script will have an array called $pupils that will contain all those values.