1

Here's what I'm getting:

    food_name_token Pizza
    id  1
    category_id 1
    review  {"note"=>"tasty!", "image"=>"<RKParamsAttachment: 0x7834500>"}

And this is what I'm expecting:

    food_name_token Pizza
    id  1
    category_id 1
    review  {"note"=>"tasty!", "image"=>{"filename"=>"image", "type"=>"image/png", "name"=>"image", "tempfile"=>"#", "head"=>"Content-Disposition: form-data; name=\"image\"; filename=\"image\"\r\nContent-Type: image/png\r\n"}}

I'm using Restkit and I'm trying to serialize my data on the Iphone and send it to my Rails backend. For the most part, it works, but I just can't seem to get my multipart image to serialize.

The image belongs to a 'Review' class, which in turn is nested in a 'Dish' class. Here is what I have so far:

// Set up routes
    RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://postbin.org"];
    [manager.router routeClass:[Dish class] toResourcePath:@"/5a850fe1" forMethod:RKRequestMethodPOST];  
    [manager.router routeClass:[Review class] toResourcePath:@"/5a850fe1" forMethod:RKRequestMethodPOST]; 

// Map Dish Object
    RKObjectMapping *dishMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
    [dishMapping mapKeyPath:@"id" toAttribute:@"identifier"];
    [dishMapping mapKeyPath:@"category_id" toAttribute:@"categoryID"];
    [dishMapping mapKeyPath:@"food_name_token" toAttribute:@"name"];

// Map Review Object
    RKObjectMapping *reviewMapping = [RKObjectMapping mappingForClass:[Review class]];
    [reviewMapping mapKeyPath:@"note" toAttribute:@"note"];
    [reviewMapping mapKeyPath:@"image" toAttribute:@"image"];

// Define the relationship mapping
    [dishMapping mapKeyPath:@"review" toRelationship:@"review" withMapping:reviewMapping];

// Set serialization for Dish Class
    RKObjectMapping* dishSerializationMapping = [dishMapping inverseMapping];
    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:dishSerializationMapping forClass:[Dish class] ]; 

// Set serialization for Review Class 
    RKObjectMapping* reviewSerializationMapping = [reviewMapping inverseMapping];
    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:reviewSerializationMapping forClass:[Review class] ]; 

Then I assign the values:

    [Dish sharedDish].name = @"Pizza";
    [Dish sharedDish].identifier = [NSNumber numberWithInt:1]; 
    [Dish sharedDish].categoryID = [NSNumber numberWithInt:1];

    [Review sharedReview].note = @"tasty!";

// Turn an image into an RKParamsAttachment object and assign it to the image attribute of the Review class
    RKParams* params = [RKParams params];
    UIImage* imageOrig = [UIImage imageNamed:@"pizza.jpg"];
    NSData* imageData = UIImagePNGRepresentation(imageOrig);
    RKParamsAttachment* image = [params setData:imageData MIMEType:@"image/png" forParam:@"image"];
    [params release];

    [Review sharedReview].image = image;

And then I assign my Review object to my Dish object's review attribute and send it off:

    [Dish sharedDish].review = [Review sharedReview];  
    [[RKObjectManager sharedManager] postObject:[Dish sharedDish] delegate:self];

It seems like I'm attaching the image to the object the wrong way.

What's the right way to do it?

Jonathan Chiu
  • 1,637
  • 2
  • 16
  • 25
  • it seems you deleted your previous answer. I think you use the RKParamsAttachment in an unsupported way. There is an example of RKParamsAttachment in RestKit RKCatalog example project. Did you tried that one out? – mja Nov 20 '11 at 18:23
  • Yup, I referenced that example. The problem is that I can't figure out how to use this line properly: `[params setData:imageData MIMEType:@"image/png" forParam:@"image"];` I need to assign something like `{review => image}` instead of just `@"image"` to the 'forParam' parameter, because the image attribute belongs to the 'Review' object, which is nested in the 'Dish' object. But it is the 'Dish' object that I'm posting. Do you know if the 'forParam' parameter can take a key that is more than one level deep? That might solve the problem... – Jonathan Chiu Nov 21 '11 at 15:26

1 Answers1

0

Did you try manually adding the nesting to the param name:

RKParamsAttachment* image = [params setData:imageData 
    MIMEType:@"image/png" forParam:@"review[image]"];
drewish
  • 9,042
  • 9
  • 38
  • 51