Nathan Borror

Sending Multipart Forms with Objective-C — It took me a few evenings to figure this out1 so I'm writing a quick explanation based on what I've found to work. My use-case is pretty simple, I want to POST some data to a form on a server from an iOS app I'm building. I'll be using NSURLRequest to build the request object and NSURLConnection to make the actual connection to the server.

The first thing we need to understand is how Multipart Form requests should be structured. The spec document explains these in detail so I'm just going to show at a high level how this is structured. To get started lets create an NSMutableURLRequest:

Now we need to define the content-type and a boundary string. I'm not really sure what a boundary string is, it just needs to be consistently represented throughout the request.

Content-Type: multipart/form-data; boundary=YOUR_BOUNDARY_STRING

To do this in objective-c we need to write the following:

Now let's get look at the request body. Here's a simple example of how it needs to look to the server:

I'm sending over three variables: an image named photo, a string named message, and an integer named user. It's important to note the linebreaks and the dashes before the boundary string. These must be included in order to build a good request. Now lets write some objective-c:

Now all we need to do is make a connection to the server and send the request:

That's it. A more detailed explanation of the request formatting can be found here.

If you're curious, I'm posting to a Django app I was running locally so I could use ipdb and iPython to inspect the process and see what my app was posting. Then I compared that to a post request generated by a testcase I knew worked.