Upload a photo from iphone with 64 encoding multi part form data

So, this wasn’t easy.

I Googled and Googled and got nowhere.  Hopefully because of this post you didn’t look as hard.

Eventually I found this a post on the OLBTwitpicEngine source code.  GREAT! I thought, until it didn’t work.

I had tried doing this on my own accord with a rails app; upload an iPhone photo to a rails server.  I got the most bizarre error I have seen.

/!\ FAILSAFE /!\  Tue Mar 03 10:13:14 +1300 2009
Status: 500 Internal Server Error
bad content body

So I tried OLB’s code, and I got the same error!  So I hacked away at my rails source code (the bit that processes a web request) so that it would spit out more details.  I had no idea what the multipart form data was meant to look like, so how could I make sure it was right!

Eventually after a day of trial and error I figured out how the form data is meant to look like, and I got it working.  Turns out the two new line characters at the beginning weren’t required.  So you can go over to lingonikorgsource.wordpress.com and get yourself a copy of the code, then remove the first two new line chars and make it look like thusly

[postBody appendData:[[NSString stringWithFormat:@"–%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

I would attach my code but I have hacked it to shreads and made it work how I need it.  You will notice that their code makes use of threading, which I mention in my last post.  Look how it’s done and you can put this photo upload with the previously mentioned post’s loading screen (which is exactly what I have done).

I will post about threading, but I will only be analysising how the above source code works and reporting from there.  I could try some concurrency things as a couple experiments I suppose..  Concurrency is fun, isn’t it!! Say yes.

Posted on March 3, 2009 at 9:23 am by Jordan Carter · Permalink
In: Iphone · Tagged with: 

6 Responses

  1. Written by Jordan
    on March 4, 2009 at 12:03 am
    Permalink

    Thanks to lingonikorgsource.wordpress.com for the kudos and link.

  2. Written by Arrel
    on June 18, 2009 at 2:15 pm
    Permalink

    One problem I rant into, the browser is converting the double dash before the boundary into an mdash:
    @”–%@\r\n” instead of
    @”–%@\r\n”

    Changing that got it working. Thanks!

  3. Written by Arrel
    on June 18, 2009 at 2:16 pm
    Permalink

    ….er, and it did it in my comment, too.

  4. Written by Ben Lachman
    on June 18, 2009 at 7:51 pm
    Permalink

    I think this is a bug with Ruby’s multi part parser. PERL will handle the leading ‘\r\n’ just fine and the RFC on the topic (2046) states:

    ‘The boundary delimiter MUST occur at the beginning of a line, i.e., following a CRLF, and the initial CRLF is considered to be attached to the boundary delimiter line rather than part of the preceding part. The boundary may be followed by zero or more characters of linear whitespace. It is then terminated by either another CRLF and the header fields for the next part, or by two CRLFs, in which case there are no header fields for the next part.’

    e.g. it is correct to have that \r\n at the beginning of the boundary line.

  5. Written by tewks
    on July 31, 2009 at 2:19 am
    Permalink

    cheers for this

  6. Written by Matt
    on May 12, 2011 at 4:24 am
    Permalink

    This post helped a lot connecting an iPhone to a Ruby server. One thing I did have to do differently was change:

    “Content-Type: image/jpg

    to

    “Content-Type: image/jpeg

    Thanks!

Leave a Reply