I have 2 questions regarding ASIHTTPRequest
1.) Can i send an array through a POST to a web service ?
like;
NSArray *arr = [[NSArray alloc]init];
NSURL *url = [NSURL URLWithString:@"http://t2.com/p.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:arr forKey:@"peoplearray"];
[request setDidFinishSelector:@selector(done:)];
[request setDelegate:self];
[request startAsynchronous];
If you see, I am passing the array like;
[request setPostValue:arr forKey:@"peoplearray"];
Is this correct ? if not could you suggest a better working approach ?
That probably won't work as expected. You would have to encode the array to JSON or some other format. I prefer JSON because there are plenty of server-side libraries to work with it. This snippet below requires this JSON framework.
[request setPostValue:[arr JSONRepresentation] forKey:@"people"];
iOS 5 comes with inbuilt JSON classes. Checkout this article for working with JSON in iOS 5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With