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?
I can show you an example of uploading a .txt file to a server with
NSMutableURLRequest
andNSURLSessionUploadTask
with help of a php script.php Script
XJones' answer worked like charm.
But he didn't mentioned/declared variables
_params
,BoundaryConstant
andrequestURL
. So, i thought of posting that part as an add-on to his post, so that it may help others in future.As i mentioned earler, this is not an answer by itself, just an addon to XJones' post.
here's the working swift code translated from the code provided by @xjones. Thanks alot for your help mate. Yours was the only way that worked for me. I used this method to send 1 image and a another parameter to a webservice made in asp.net
For http post image and username and password sending through post method
Use AFNetworking; Put other parameters in the parameter dictionary and append the image data in form data.