3

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 :)

1

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

work like a charm and solve our problem which took us few hours
1

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]}

2 Comments

No visible @interface for 'NSDictionary' declares the selector 'JSONRepresentation' have you seen this error?
hey, were you able to figure out that error? it's doing the same thing for me.
0

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?

2 Comments

I am using this JSON library: stig.github.com/json-framework your example worked after I changed the start of your string to task={\"id\":
JSON framework is a well known library. Though I use JSONkit, I believe JSON framework does the job correctly and returns well-formed strings. Thus the problem is either on the server side or in the objects which you pass to the JSON framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.