sharon
sharon

Reputation: 580

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 ?

Upvotes: 3

Views: 2584

Answers (2)

Webber Lai
Webber Lai

Reputation: 2022

try this It works for me

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:ListPlacesURL];
for (int i =0; i < [placeTagID count]; i++) {
        [request setPostValue:[[data objectAtIndex:i] objectForKey:@"id"] forKey:[NSString stringWithFormat:@"ids[%i]",i]];
}
[request startSynchronous];

Upvotes: 1

Anurag
Anurag

Reputation: 141859

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.

Upvotes: 3

Related Questions