0

I'm having a serious problem and searched for many hours without finding a solution.

I have a backend server using Ruby of Rails and paperclip for image saving.

The image upload works fine from the browser, POSTing to /pictures

But when I try to upload it to /pictures.json using a multipart request with RestKit (iOS), I get a 406.

When the image (Picture object) is created server-side, a reward (Reward object) is associated to it. This reward object is what I'm expecting as a JSON response.

Here is the Objective-C code:

NSMutableURLRequest* uploadRequest = [self.objectManager multipartFormRequestWithObject:nil method:RKRequestMethodPOST 
path:@"/pictures.json" 
parameters:nil 
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData 
         appendPartWithFileData:UIImagePNGRepresentation(socialReward.image)
         name:@"picture[pict]" 
         fileName:imageName 
         mimeType:@"image/png"];
 }];

Here is the controller:

@picture = Picture.create( params[:picture] )

respond_to do |format|
    format.html {
        redirect_to @picture.reward
    }
    format.js {
        render :json => @picture.reward.to_json(:include => :picture)
    }
end

The CSRF check is disabled.

When comparing the requests from the browser and iOS using Wireshark, they are extremely similar.

Thanks in advance for your help.

David
  • 9,635
  • 5
  • 62
  • 68
Loïc Gardiol
  • 361
  • 3
  • 7
  • Similar - what's different? Is RestKit setting the accept header to show that it expects JSON to be returned from the server? – Wain May 13 '13 at 08:54
  • Thanks for your reply. The body is the same (except different boundaries names and authenticity_token that is present in the request from the browser). The Accept in the browser request is "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8". In RestKit it is "application/json". But this is correct, as I'm expecting JSON in response. – Loïc Gardiol May 13 '13 at 08:59

1 Answers1

1

The problem is with the RoR, in the respond_to you should be using format.json, not format.js.

Wain
  • 118,658
  • 15
  • 128
  • 151