RestKit Image Upload

Jacob picture Jacob · Dec 8, 2011 · Viewed 7.2k times · Source

I am using RestKit to drive interactions with my web server. I am trying to use routing to POST an Event object to the server with an image attached to it. The code is as follows:

  RKObjectManager *manager = [RKObjectManager sharedManager];

  RKObjectMapping *map = [self eventMapping];
  manager.serializationMIMEType = RKMIMETypeFormURLEncoded;
  map.rootKeyPath = @"event";
  [manager.mappingProvider setSerializationMapping:map forClass:[Event class]];
  [manager.router routeClass:[Event class] toResourcePath:@"/v1/events.json" forMethod:RKRequestMethodPOST];

  [manager postObject:event delegate:self block:^(RKObjectLoader *loader){
    RKObjectMapping *serMap = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[Event class]];
    NSError *error = nil;
    NSDictionary *d = [[RKObjectSerializer serializerWithObject:event mapping:serMap] serializedObject:&error];

    RKParams *p = [RKParams paramsWithDictionary:d];
    [p setData:[event imageData] MIMEType:@"image/jpeg" forParam:@"image"];
    loader.params = p;
  }];

If I create an instance of RKParams using the serialized Event object, then add the image data and assign it as the RKObjectLoader's params property, all the properties become one massive serialized string. There must be a way to upload an image without the massive string serialization.

I have also tried having an NSData property that is mapped to some attribute, converting a UIImage into NSData along the way, but RestKit complains that it can't be mapped. Has anyone done this before?

Answer

Michael Peterson picture Michael Peterson · Dec 10, 2011

I did something very similar and it worked out just fine. I realize your question is about why RKObjectSerializer isn't working the way you expect, but maybe it is something else with your setup. I'm posting my code to give a clean example of something that does work. That said, after reading the RKObjectSerializer documentation, I don't see why you couldn't initialize your RKParams that way instead of setting them directly as I do in my example.

Router setup:

RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:kApiUrlBase];
[objectManager.router routeClass:[PAPetPhoto class] toResourcePath:@"/pet/uploadPhoto" forMethod:RKRequestMethodPOST];

Mapping setup:

RKObjectMapping *papetPhotoMapping = [RKObjectMapping mappingForClass:[PAPetPhoto class]];
[papetPhotoMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[papetPhotoMapping mapAttributes:@"accountId", @"petId", @"photoId", @"filename", @"contentType", nil];
[objectManager.mappingProvider addObjectMapping:papetPhotoMapping];
[objectManager.mappingProvider setSerializationMapping:[papetPhotoMapping inverseMapping] forClass:[PAPetPhoto class]];
[objectManager.mappingProvider setMapping:papetPhotoMapping forKeyPath:@"petPhoto"];

The post: (notice since I built up all my params in the block my object is just a dummy instance to trigger the proper routing and mapper).

    PAPetPhoto *photo = [[PAPetPhoto alloc] init];
    [[RKObjectManager sharedManager] postObject:photo delegate:self block:^(RKObjectLoader *loader){

        RKParams* params = [RKParams params];
        [params setValue:pet.accountId forParam:@"accountId"];
        [params setValue:pet.identifier forParam:@"petId"];
        [params setValue:_photoId forParam:@"photoId"];
        [params setValue:_isThumb ? @"THUMB" : @"FULL" forParam:@"photoSize"];
        [params setData:data MIMEType:@"image/png" forParam:@"image"];

        loader.params = params;
    }];

Server endpoint (Java, Spring MVC)

    @RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> handleFormUpload(@RequestParam("accountId") String accountId,
                                    @RequestParam("petId") String petId,
                                    @RequestParam("photoId") String photoId,
                                    @RequestParam("photoSize") PhotoSizeEnum photoSize,
                                    @RequestParam("image") Part image) throws IOException {

        if (log.isTraceEnabled()) 
            log.trace("uploadPhoto. accountId=" + accountId + " petId=" + petId + " photoId=" + photoId + " photoSize=" + photoSize);

        PetPhoto petPhoto = petDao.savePetPhoto(accountId, petId, photoId, photoSize, image);

        Map<String, Object> map = GsonUtils.wrapWithKeypath(petPhoto, "petPhoto");
        return map;
    }

Server response JSON (note the keyPath of "petPhoto" that corresponds to the mapping setup):

{
    petPhoto =     {
        accountId = 4ebee3469ae2d8adf983c561;
        contentType = "image/png";
        filename = "4ebee3469ae2d8adf983c561_4ec0983d036463d900841f09_3FED4959-1042-4D8B-91A8-76AA873851A3";
        id = 4ee2e80203646ecd096d5201;
        petId = 4ec0983d036463d900841f09;
        photoId = "3FED4959-1042-4D8B-91A8-76AA873851A3";
    };
}

Delegate:

- (void) objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object {

    if ([objectLoader wasSentToResourcePath:@"/pet/uploadPhoto"]) {
       PAPetPhoto *photo = (PAPetPhoto*)object;
    }
}