POST multipart/form-data with Objective-C for image and other parameter

I've looked into a good number of articles on how to do a multipart/form-data POST on iOS, but none really explain what to do if there were normal parameters as well as the file upload.



I have used the multipart/form-data POST on iOS & I had written the following code and it is uploading data but not image data


- (void)postWithImage:(NSDictionary *)dictionary

{


NSString *urlString = [NSString stringWithFormat:@"YourHostString"];


NSURL *url = [NSURL URLWithString:urlString];

NSString *boundary = @"----1010101010";

// define content type and add Body Boundry

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

NSEnumerator *enumerator = [dictionary keyEnumerator];

NSString *key;

NSString *value;

NSString *content_disposition;

while ((key = (NSString *)[enumerator nextObject])) {

if ([key isEqualToString:@"file"]) {

value = (NSString *)[dictionary objectForKey:key];

NSData *postData = UIImageJPEGRepresentation([UIImage imageNamed:value], 1.0);

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";\r\nfilename=\"screen.png\"\r\n\r\n",value] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:postData];

} else {

value = (NSString *)[dictionary objectForKey:key];

content_disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key];

[body appendData:[content_disposition dataUsingEncoding:NSUTF8StringEncoding]];

NSError *error;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:value options:NSJSONWritingPrettyPrinted error:&error];

[body appendData:jsonData];

//[body appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];

}

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

}

//Close the request body with Boundry

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

[request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"%@", returnString);

}


Can anyone please help me to get why image data is not uploading

To start, please stop using

NSURLConnection
. It’s been deprecated in favour of
NSURLSession
for several years now.

With regards your form question, you’ll need two things to resolve this issue:

  • An understanding of how

    multipart/form-data
    is supposed to be formatted (A)
  • A dump of the data that you’re creating (B)

Once you have these you can compare the two to find the differences and adjust B accordingly.

With regards A, I recommend that you check out RFC 2388. Also, if your form is usable by a web browser, you can capture its request to see an example of something acceptable to the web server.

With regards B, you can hex dump the data (

body
in your code snippet) and proceed from there.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Click me!

POST multipart/form-data with Objective-C for image and other parameter
 
 
Q