Restkit MultiForm Image Post

Problem

I am trying to send a multi-format request containing an image attachment to the server. I could not get the image on the server, this is other information that is not sent correctly.

More details

I use object mapping to configure several different attributes when retrieving 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 the photo, I update the configuration 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 a photograph. Here is how I post it, like 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 receives the image as planned, and actually receives the "created" and "participants", unfortunately, in a strange format that the server does not understand. It includes line breaks and such participants (\n 19843589323 \n created: \n 3-31-2012 00:00 (something like this, I will update when I have access to the logs.

I will give you any additional information that you need. If I had enough to do this, I would call it a reputation;)

+7
source share
2 answers

In RestKit 0.20.0-pre3 , RKObjectManager has a multipartFormRequestWithObject:method:path:parameters:constructingBodyWithBlock:

+14
source

An example of this task can be found on 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 
+11
source

All Articles