Using Swift in an iOS Application to Upload an Image to a RESTful API

October 11, 2014

Last time, I wrote a post about how to create a RESTful API using Rails and Paperclip. The second part of my task is to figure out a way on how to upload an image taken from my phone’s Camera using Swift.

To send the request, I used NSMutableURLRequest to handle the JSON request

After the request has completed, the block I passed via the completionHandler will be executed. From there, I was able to evaluate the response. So that’s how I was able to send the image taken from my phone’s camera to the server.

UPDATE: I modified the post to use UIImageJPEGRepresentation instead of UIImagePNGRepresentation as suggested by Paul Senzee. I also added more detail to make the sample code clearer.