Search code examples
iosrestkitrestkit-0.20

Post JSON Body + MultiPart RestKit 0.2x fails


I am trying to post an object with an attached file.

 NSMutableURLRequest *request =
            [objectManager multipartFormRequestWithObject:reqDocObj
                                                   method:RKRequestMethodPOST
                                                     path:@"syncDocument.json"
                                               parameters:nil
                                constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                    [formData appendPartWithFileData:UIImagePNGRepresentation([UIImage imageNamed:@"graybox.png"])
                                                                name:@"image"
                                                            fileName:@"some_file"
                                                            mimeType:@"image/jpeg"];

            }];



RKObjectRequestOperation *operation =
            [objectManager
             objectRequestOperationWithRequest:request
                                       success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {


                                       }  
                                    failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                          NSLog(@"WS: errore operazione di richiesta %@",error);


                                        }  
             ];  


            [objectManager enqueueObjectRequestOperation:operation];

The objectManager is configured as:

    [objectManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
    [objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
    objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

[EDIT]

My mapepd object is SynchDocObj:

requestDocMapping = [RKObjectMapping mappingForClass:[SynchDocObj class]];
[requestDocMapping addAttributeMappingsFromDictionary:mappingDocDict];

The problem is:

1) In the RKlogs, the request.body = null and the JSON object is put into the form-data

2) The server cannot decode the body because it is null

My question is:

1) Am I sending the JSON object in the wrong way?

2) If yes, how can I send a JSON object with a file upload, i.e. as a multipart request? Regards!

[SOLUTION]

Following the suggestion of the answer, I think the solution is 1) retrieve the mapped object from the form-data and not the body ; 2) OR post a nil object and putting a JSON string within the form-data.


Solution

  • This:

    [objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
    objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
    

    is 2 different ways of saying the same thing - and you don't want to have either of them. You need to send the request as form URL encoded (the default value).

    The easiest thing to do is to use the same form as in your current code to create the request, generate the JSON earlier and then use appendPartWithFormData:name: to add it to the request (just before you add the file).

    To generate the JSON you can use RestKit (RKMappingOperation) or you can just create a dictionary / array of content and then use NSJSONSerialization to serialise that object to be added to the request.