Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone sending Json Array via POST

I need to send the following JSON array via POST to our server:

task:{"id":"123","list":"456","done":1,"done_date":1305016383}

I tried with the JSON library, but I was somehow to stupid to use it. I even tried to build up the POST-String by myself, but also failed:

NSString *post = @"task='{id:123,list:456,done:1,done_date:1305016383}'";

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url 
                                               cachePolicy:NSURLRequestReloadIgnoringCacheData    
                                            timeoutInterval:30];

[request setHTTPMethod:@"POST"];    
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
....

Can you please help me? The json’ed POST string would be even enough for me :)

like image 989
Sebastian Avatar asked Mar 14 '26 11:03

Sebastian


1 Answers

So this may or may not be the question you are asking, but your JSON string is not formed correctly. An array of "task" in JSON format would look like this:

NSString *post = @"{"task":[{"id":"123","list":"456","done":1,"done_date":1305016383}]}";

I was just wresting with a similar situation posting to a PHP server and I couldn't find any questions about it online, but this is what I would've had to do if I were posting the same data:

NSString *post = @"task[0][id]=123&task[0][list]=456&task[0][done]=1&task[0][done_date]=1305016383&";

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url 
                                           cachePolicy:NSURLRequestReloadIgnoringCacheData    
                                        timeoutInterval:30];

[request setHTTPMethod:@"POST"];    
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
...

Good luck!

like image 184
Matt W. Avatar answered Mar 16 '26 23:03

Matt W.