I know this may seem specific to the issue at hand, however, this is me trying to avoid the horrible UploadCare usage of cocoa pods which contains a significant (and rather outdated) amount of dependencies, to JUST upload a file (and subsequently download it at a later date).
I'm struggling to translate the '-F' options in the cURL command. I understand that they specify HTTP multipart POST data, but converting this into NSMutableData with the picture file attached is difficult. I keep receiving a 403 status code.
The cURL command is:
curl -F "UPLOADCARE_PUB_KEY=e84a031b3da1g560d56d" \
-F "UPLOADCARE_STORE=1" \
-F "file=@aaronmillman.jpg" https://upload.uploadcare.com/base/
My current attempt is:
NSMutableData *body = [NSMutableData data];
NSData *imageData = UIImageJPEGRepresentation(image, 0.6);
if (imageData) {
[body appendData:[@"UPLOADCARE_PUB_KEY=e84a031b3da1g560d56d" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"UPLOADCARE_STORE=1" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"file="] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
}
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:baseUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = imageData;
NSURLSessionDataTask *uploadTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@",response);
}];
[uploadTask resume];
What am I doing wrong with the NSMutableData?
A second, and related question is: Is it worth using an objective-C lib curl wrapper at all?