Reputation: 9051
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 :)
Upvotes: 3
Views: 4938
Reputation: 320
This is how I handle It:
-(void)imageFactory:(NSDictionary*)postJSON
{
// Convert the JSON string to Data to be sent.
NSData* postData = [[postJSON JSONRepresentation] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[URLManager imageFactory]]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
// IMPORTANT MAKE SURE YOU ADD THIS LINE SO THE SERVER KNOWS WHAT ITS GETTING
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
// Add some nice handlers so you know what you got from the server
NSURLResponse* response;
NSHTTPURLResponse* httpResponse;
NSError* error;
NSData* responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* stringResponse = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
httpResponse = (NSHTTPURLResponse*) response;
int statuscode = [httpResponse statusCode];
if (statuscode == 200)
{
log4Debug(@"ImageFactory Response Successful, Retrieving image");
// Handle the response here if needed
}
else
{
log4Error(@"ImageFactory Response Failed: %@", stringResponse);
// Show some form of alert here if needed
}
// release all objects saved to memory
[request release];
request = nil;
[stringResponse release];
stringResponse = nil;
}
My json is a string that looks like this {"user":1337,"title":"Some title","itemKeys":[1,1,1,1,1]}
Upvotes: 1
Reputation: 956
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!
Upvotes: 1
Reputation: 777
I suppose the problem is not with the methods, but with the string you're trying to post. Try this one:
NSString *post = @"\"task\":{\"id\":\"123\",\"list\":\"456\",\"done\":1,\"done_date\":1305016383}";
In other words: experiment with the quotation marks.
What JSON library are you using?
Upvotes: 0