Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use ASIFormDataRequest to http post array of NSDictionary objects

Hey, I need to make a HTTP POST request with an array of NSDictionary objects.

When I do this, however, I notice on the server side that the NSDictionary object does not get deserialized to a hash. It gets deserialized to a string - which is not what i want.

This is how I send the parameter from the client (IPhone) side:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
for (ABContact *c in contactsWithEmailsOrPhones){
  NSString *phoneNumber = [[ABContactsHelper class] contactPhoneNumber:c];
  NSString *email = [[c emailArray] objectAtIndex:0];
  NSLog(@"looping: %@, %@", phoneNumber, email);
  NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                         phoneNumber,
                         @"phone", 
                         email,
                         @"email", 
                         [c firstname],
                         @"firstname",
                          [c lastname],
                          @"lastname", nil];

    [request addPostValue:dict forKey:@"contacts[]"];
}

[request setDelegate:self];
[request startAsynchronous];

This is how it looks when it is deserialized on the server (rails) side:

Started POST "/app/find_friends" for 67.164.97.48 at Thu Sep 23 14:40:37 -0700 2010
Processing by app#find_friends as HTML
Parameters: {"contacts"=>["{\n    email = \"xx\";\n    firstname = xx;\n    lastname = xx;\n    phone = \"xx\";\n}", "{\n    email = \"xx\";\n    firstname = xx;\n    lastname = xx;\n    phone = \"xx\";\n}"]}
Completed 200 OK in 0ms (Views: 0.3ms | ActiveRecord: 0.0ms)

I am sure this is a common problem that people face. So there definitely is a solution for this.

Thanks in advance for all the comments/answers.

like image 216
amehta Avatar asked Sep 24 '10 04:09

amehta


1 Answers

Firstly, to explain what you're seeing:

The addPostValue method is designed to take a single value, and calls [value description] to convert that value into a string. The description method gives a string representation, which is what you're seeing on the server side (the description method is also what NSLog calls if you pass an object to it, so the format should look quite familiar). So the iphone has being asked to send a string, and does so, and the server is "correctly" showing that as a string.

If you just want to send a single contact, then just call 'addPostValue' for each key/value pair - ie. once for phone, again for email, etc.

If you want to pass multiple contacts you probably need something better. I'm not really familiar with ruby-on-rails so there may be other solutions, but certainly a common way to pass more complex data structures to webservices from the iphone is to use json.

There's a json library for the iphone here:

http://code.google.com/p/json-framework/

and ruby on rails has built in json support.

like image 197
JosephH Avatar answered Oct 28 '22 23:10

JosephH