I need to upload a file to a server and the request has to specify some form parameters. How can I do this with the NSURLSessionUploadTask? In other words, how can I send a multipart POST request.
cheers
I need to upload a file to a server and the request has to specify some form parameters. How can I do this with the NSURLSessionUploadTask? In other words, how can I send a multipart POST request.
cheers
When using NSURLSessionUploadTask
, you don't call setHTTPBody
on the NSMutableURLRequest
(like we used to do with NSURLConnection
), but rather you include this in the NSData
or file passed to the NSURLSession
method, uploadTaskWithRequest
(or in the stream we supply for uploadTaskWithStreamedRequest
).
In terms of creating the body of the request, itself, you have to do this yourself. Obviously, a framework like AFNetworking can greatly simplify this process.
https://github.com/pyke369/PKMultipartInputStream
This library can generate the multipart request pretty well.