Thanks for reading.
I am new to iOS and I am trying to upload an Image and a text using multi-part form encoding
in iOS.
The curl
equivalent is something like this: curl -F "param1=value1" -F "param2=@testimage.jpg" "http://some.ip.address:5000/upload"
The curl
command above returns the expected correct response in JSON.
Problem: I keep getting a HTTP 400 request which means I am doing something wrong while composing the HTTP POST Body.
What I Did: For some reference, I tried Flickr API iOS app "POST size too large!" and Objective C: How to upload image and text using HTTP POST?. But, I keep getting a HTTP 400.
I tried the ASIHttpRequest
but had a different problem there (the callback never got called). But, I didn't investigate further on that since I've heard the developer has stopped supporting the library: http://allseeing-i.com/[request_release];
Could someone please help me out?
Here is a Swift version. Note that if you do not want to send form data it is still important to send the empty form boundary. Flask in particular expects form data followed by file data and will not populate
request.files
without the first boundary.Here's code from my app to post an image to our web server:
Here is my similar network kit library for uploading files as multipart form:
Use the delegate for notifying about uploading progress.
Use the notificationName for notifying when request has finished.
Use the queue for adding this request into your operation queue so it will be processed in right time.
Upload image with form data using NSURLConnection class in Swift 2.2:
Note: Always use
sendAsynchronousRequest
method instead ofsendSynchronousRequest
for uploading/downloading data to avoid blocking main thread. Here I used sendSynchronousRequest for testing purpose only.