2

I am having problem of POSTing data with RestKit. It is actually syncing of Array DBMeetings objects (created offline) when network is available. DBMeetings objects can have Photo attachments, so I am using following method by RestKit.

multipartFormRequestWithObject:method:path:parameters:constructingBodyWithBlock:

Problem is multipartFormRequest cannot retain JSON structure of data, that is why I am creating a JSON string and POSTing it as parameter, so that server side can parse appropriately. I found this idea from following SO's answer https://stackoverflow.com/a/19545138/1444321

NSString *jsonString = [self jsonFromArray:unSyncedMeetings];
NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:nil
                                                                                                method:RKRequestMethodPOST
                                                                                                  path:URL_MEETINGS_SYNC
                                                                                            parameters:@{@"total_files": [NSNumber numberWithInt:totalFiles], @"data": jsonString} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// just adding photo attachments in this block
}

If I log jsonString it displays like this.

[{"id":92172,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-06T15:23:00Z","tags":[],"title":"abc","sample":false,"start_time":"2014-11-06T14:23:00Z","organizer":{"name":"Mithooo Popla","id":42900,"user_id":0,"email":"testacc090@abc.com"},"updated_at":"2014-11-06T14:25:34Z","meeting_topics":[{"id":800881,"title":"Tippih","meeting_id":92172,"type":"Topic","tags":[],"updated_at":"2014-11-06T14:25:34Z","created_at":"2014-11-06T14:23:47Z","children":[{"id":800882,"total_attachments":4,"topic_id":800881,"additional_owners":[],"title":"Tyhh\nVdh","tags":[],"type":"Note","attachments":[{"id":26291,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26291-IMG_0371-b100db1da64b5c001bafb629b32bfffaee000ecb.PNG?1415283828","file_name":"IMG_0371.PNG"},{"id":26289,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26289-IMG_0365-a7e2ee7f013b47d99a2ad06e95d7968676108f2b.PNG?1415283827","file_name":"IMG_0365.PNG"},{"id":26290,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26290-IMG_0364-c5ae106dd58ec4c31b3e0fc82721da1cee0444b7.JPG?1415283827","file_name":"IMG_0364.JPG"},{"id":26292,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26292-IMG_0366-0131a80b2199b6cc4d0d684f3acb78bc7a9c78a3.PNG?1415283828","file_name":"IMG_0366.PNG"}],"send_email":false,"updated_at":"2014-11-06T14:23:47Z","private":false,"created_at":"2014-11-06T14:23:47Z","user_id":42900,"deleted":false,"position":1,"unique_parent_id":"x-coredata:\/\/442A8ECF-97C6-493E-93D6-62BEBDEF5064\/DBTopics\/p2","status":0,"total_comments":0}],"presenter":{"user_id":0},"user_id":42900,"deleted":false,"subtopics":[],"parent_id":0,"position":1,"unique_id":"x-coredata:\/\/442A8ECF-97C6-493E-93D6-62BEBDEF5064\/DBTopics\/p2"}],"created_at":"2014-11-06T14:23:10Z","lock":false,"deleted":false,"required_participants":[]},{"id":91976,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-22T22:04:00Z","location":"","finish_date":"2014-11-22T22:04:00Z","sample":false,"start_time":"2014-11-22T21:04:00Z","title":"aaaaaa 25 we'd future date. hggggg","updated_at":"2014-10-31T13:47:46Z","tags":[],"created_at":"2014-10-31T13:47:45Z","lock":false,"organizer":{"name":"mr  shake ","id":42902,"user_id":0,"email":"awaisnxb3@gmail.com"},"meeting_topics":[],"deleted":false,"required_participants":[{"name":"mr  shake ","id":42902,"user_id":0,"email":"awaisnxb3@gmail.com"}],"start_date":"2014-11-22T21:04:00Z"},{"id":92187,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-07T14:04:00Z","location":"","finish_date":"2014-11-07T14:04:00Z","sample":false,"start_time":"2014-11-07T13:04:00Z","title":"hhhhhhhhhh ggggjdfd,f hhh","updated_at":"2014-11-07T15:00:14Z","tags":[],"created_at":"2014-11-07T13:04:12Z","lock":false,"organizer":{"name":"mr  shake ","id":42902,"user_id":0,"email":"awaisnxb3@gmail.com"},"meeting_topics":[],"deleted":false,"required_participants":[{"name":"mr  shake ","id":42902,"user_id":0,"email":"awaisnxb3@gmail.com"}],"start_date":"2014-11-07T13:04:00Z"},{"id":92185,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-07T12:49:37Z","location":"","finish_date":"2014-11-07T12:49:37Z","sample":false,"start_time":"2014-11-07T11:49:37Z","title":"jgfjfhtbn gggggggg","updated_at":"2014-11-07T11:49:37Z","tags":[],"created_at":"2014-11-07T11:49:37Z","lock":false,"organizer":{"name":"mr  shake ","id":42902,"user_id":0,"email":"awaisnxb3@gmail.com"},"meeting_topics":[],"deleted":false,"required_participants":[{"name":"mr  shake ","id":42902,"user_id":0,"email":"awaisnxb3@gmail.com"}],"start_date":"2014-11-07T11:49:37Z"}]

When I check request with Charles, it is still correct. Here is the screenshot from Charles. enter image description here

But on the server, it is accessed as

data="{\"meetings\":[{\"id\":91160,\"total_topics\":0,\"optional_participants\":[],\"readonly_participants\":[],\"finish_time\":\"2014-11-07T13:47:17Z\",\"location\":\"\",\"finish_date\":\"2014-11-07T13:47:17Z\",\"sample\":false,\"start_time\":\"2014-11-07T12:47:17Z\",\"title\":\"his 8898997998897897\",\"updated_at\":\"2014-11-07T12:47:27Z\",\"tags\":[],\"created_at\":\"2014-11-07T12:47:17Z\",\"lock\":false,\"organizer\":{\"name\":\"Rehmat Qadir\",\"id\":12891,\"user_id\":0,\"email\":\"rehmat.mking@nxvt.com\"},\"meeting_topics\":[],\"deleted\":false,\"required_participants\":[{\"name\":\"Rehmat Qadir\",\"id\":12891,\"user_id\":0,\"email\":\"rehmat.mking@nxvt.com\"}],\"start_date\":\"2014-11-07T12:47:17Z\"}]}"

Backslashes are added on everywhere. Server side is not able to treat the value of data as JSON object, and cannot parse data. Any idea what is the actual problem? Any help is appreciated.

This is how I am creating jsonString

+ (NSString *)jsonFromArray:(NSArray *)meetings {
    NSArray *requestDescriptors = [[RKObjectManager sharedManager] requestDescriptors];
    RKRequestDescriptor *requestDescriptorObject = [requestDescriptors objectAtIndex:10];

    NSMutableDictionary *jsonDictionary = [[NSMutableDictionary alloc] init];
    NSMutableArray *jsonArray = [[NSMutableArray alloc] init];

    if (meetings.count > 0) {
        for (DBMeetings *meeting in meetings) {
            NSDictionary *parametersForObject = [RKObjectParameterization parametersWithObject:meeting requestDescriptor:requestDescriptorObject error:nil];
            NSDictionary *meetingDictionary = [parametersForObject valueForKey:@"meetings"];
            [jsonArray addObject:meetingDictionary];
        }
    }

    [jsonDictionary setValue:jsonArray forKey:@"meetings"];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonArray options:kNilOptions error:nil];

    NSString *jsonString;
    if(jsonData) {jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];}
    return jsonString;
}
Community
  • 1
  • 1
Khawar
  • 9,151
  • 9
  • 46
  • 67

1 Answers1

2

The JSON string you're creating looks fine, as does the way you're sending it and the Charles log. As such this is most likely an entirely server based issue with the processing of the text and the escaping of its contents. This should usually be transparent so you need to show how that's being handled. Worse case you can process the string and replace all of the escaped characters.

Wain
  • 118,658
  • 15
  • 128
  • 151