Note that there are some explanatory texts on larger screens.

plurals
  1. POiphone image to Ruby on Rails - bad content body, parse_multipart
    text
    copied!<p>I am trying to upload an image from the iPhone to a Ruby on Rails (RoR) server and I am getting the following error:</p> <pre><code>!\ FAILSAFE /!\ Thu Nov 11 23:51:39 CET 2010 Status: 500 Internal Server Error bad content body /usr/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:319:in `parse_multipart' </code></pre> <p>The communication to the RoR site works fine when I leave out the line below where I set the value for the string with multipart/form-data. </p> <p>iPhone code:</p> <pre><code> NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"logo1" ofType:@"png"]; NSData *imageData = [NSData dataWithContentsOfFile:imagePath]; NSString *urlString = [NSString stringWithFormat:@"%@/user/iphone_results_to_website/", serverString ]; NSMutableURLRequest *imageRequest = [[[NSMutableURLRequest alloc] init] autorelease]; [imageRequest setURL:[NSURL URLWithString:urlString]]; [imageRequest setHTTPMethod:@"POST"]; NSString *boundary = [NSString stringWithString:@"--------------------------- 14737809831466499882746641449"]; // Works fine without this next line: [imageRequest setValue: [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"]; NSMutableData *body = [NSMutableData dataWithCapacity:[imageData length] + 512]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"logo1.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [imageRequest setHTTPBody:body]; theConnection = [[NSURLConnection alloc] initWithRequest:imageRequest delegate:self]; if(theConnection) { theConnectionData = [[NSMutableData data] retain]; // theData is an instance variable } else { // failed to make connection } </code></pre> <p>On the RoR side I have RMagick, ImageMagick and attachment_fu installed and I have setup a Photo model for attachment_fu as:</p> <p>RoR Photo model:</p> <pre><code>class Photo &lt; ActiveRecord::Base has_attachment :storage =&gt; :file_system, :resize_to =&gt; '640x480', :thumbnails =&gt; { :thumb =&gt; '160x120', :tiny =&gt; '50&gt;' }, :max_size =&gt; 5.megabytes, :content_type =&gt; :image, :processor =&gt; 'Rmagick' validates_as_attachment belongs_to :user ########################################################################### def uploaded_picture=(picture_field) self.name = base_part_of(picture_field.original_fielname) self.content_type = picture_field.content_type.chomp self.data = picture_field.read end # uploaded_picture ########################################################################### def base_part_of(file_name) File.basename(file_name).gsub(/[^\w._-]/, '') end # base_part_of ########################################################################### end </code></pre> <p>Does anyone know what the problem could be? Could the problem be in my Objective-C code or in the RoR side? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload