Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restkit MultiForm Post with an Image

Problem

I've been trying to post to the server with a multiform request that includes an image attachment. I haven't had trouble getting the image to the server, it is the other information that is not sending correctly.

Details

I'm using object mapping to configure several different attributes when receiving objects from the server:

//Using a custom class to map object I receive to
RKObjectMapping * memoryMapper = [RKObjectMapping mappingForClass:[MemoContent class]];
[memoryMapper mapAttributes:@"created", @"user", @"participants", @"tags", @"text", @"kind", @"video", @"location", nil];
[memoryMapper mapKeyPath:@"_id" toAttribute:@"memoryID"];

//MediaMapper handles the data needed for the Image attachments
RKObjectMapping * mediaMapper = [RKObjectMapping mappingForClass:[MemoMedia class]];
[mediaMapper mapKeyPath:@"processed" toAttribute:@"processed"];
[mediaMapper mapKeyPath:@"original" toAttribute:@"original"];
[mediaMapper mapKeyPath:@"mime" toAttribute:@"mimeType"];
[memoryMapper mapKeyPath:@"media" toRelationship:@"rawMedia" withMapping:mediaMapper];

//
[[RKObjectManager sharedManager].mappingProvider setMapping:memoryMapper forKeyPath:@"memories"];
[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeFormURLEncoded;
[RKObjectManager sharedManager].acceptMIMEType = RKMIMETypeJSON;

Then, when it comes time to post a photo I update configurations as follows:

 RKObjectMapping * memoryMapper = [RKObjectMapping mappingForClass:[MemoContent class]];
[memoryMapper mapAttributes:@"created", @"participants",  nil];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:memoryMapper forClass:[MemoContent class]];
[[RKObjectManager sharedManager].mappingProvider setMapping:memoryMapper forKeyPath:@"memory"];

Participants are people tagged with the photo. Here is how I'm posting it, similar to this https://github.com/RestKit/RestKit/wiki/Attach-a-File-to-an-RKObjectLoader

   [[RKObjectManager sharedManager] postObject:theMemory usingBlock:^(RKObjectLoader * loader){

    RKObjectMapping* serializationMapping = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[MemoContent class]];
    NSLog(@"serializationMapping: %@", serializationMapping);
    loader.delegate = APP; //main app delegate posting, updating
    NSError* error = nil;
    RKObjectSerializer * serializer = [[RKObjectSerializer alloc] initWithObject:theMemory mapping:serializationMapping];
    NSDictionary * dictionary = [serializer serializedObject:&error];  
    RKParams * params = [RKParams paramsWithDictionary:dictionary];
    NSData * imageData = UIImagePNGRepresentation(theMemory.photo); //image data
    [params setData:imageData MIMEType:@"image/png" forParam:@"attachment"];
    loader.params = params;
    loader.serializationMIMEType = RKMIMETypeFormURLEncoded;
    }];

The server is receiving the image as planned, and actually does receive the 'created' and 'participants' unfortunately it's in a strange format that the server doesn't understand. It includes line breaks and such participants (\n 19843589323 \n created: \n 3-31-2012 00:00 (something like that, I will update when I have access to the logs.

I will give you any extra info you need. Would offer reputation for it if I had enough to do so ;)

like image 286
Rob Caraway Avatar asked Sep 26 '12 15:09

Rob Caraway


2 Answers

In RestKit 0.20.0-pre3, RKObjectManager does have method multipartFormRequestWithObject:method:path:parameters:constructingBodyWithBlock:

like image 168
pkananen Avatar answered Sep 18 '22 15:09

pkananen


An example of this task can be found at the RestKit Github page:

Article *article = [Article new];
UIImage *image = [UIImage imageNamed:@"some_image.png"];

// Serialize the Article attributes then attach a file
NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:article method:RKRequestMethodPOST path:nil parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:UIImagePNGRepresentation(image)
                                name:@"article[image]"
                            fileName:@"photo.png"
                            mimeType:@"image/png"];
}];

RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] objectRequestOperationWithRequest:request success:nil failure:nil];
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation]; // NOTE: Must be enqueued rather than started
like image 44
Robertibiris Avatar answered Sep 17 '22 15:09

Robertibiris