Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass an Array over ASIHTTPRequest - POST

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 ?

like image 305
sharon Avatar asked Mar 31 '26 17:03

sharon


1 Answers

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.

like image 155
Anurag Avatar answered Apr 03 '26 08:04

Anurag