Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
example of a multi-part form post in objective-c
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"REST URL PATH"]];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"unique-consistent-string";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@\r\n\r\n", @"imageCaption"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", @"Some Caption"] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=imageName.jpg\r\n", @"imageFormKey"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if(data.length > 0)
{
//success
}
}];
@Asha149
Copy link

Asha149 commented Oct 17, 2014

Hi. Thanks for the example. Works well.
Im facing issues uploading multiple images using the above code. It only accepts the first image. #ios

@martinrybak
Copy link

martinrybak commented Nov 5, 2014

Thanks very much for this!

@jonathanjimenez
Copy link

jonathanjimenez commented Dec 30, 2014

Thanks :D

@jonathanjimenez
Copy link

jonathanjimenez commented Dec 30, 2014

God bless you.

@datwiki
Copy link

datwiki commented Apr 13, 2015

hi, thanks for posting the code. i am sticking with 2 questions:

  1. what exactly is the object "image"
  2. name=%@; filename=imageName.jpg\r\n" => if we are selecting image from device library then what will replace the "imageName.jpg"?

Thanks much for your help

@NareshKalakuntla
Copy link

NareshKalakuntla commented May 14, 2015

Thank you 😄 This helped me a lot.

@SushilSharma12
Copy link

SushilSharma12 commented Jun 8, 2015

What to do in case of video ?
How to upload video with multipart?

@boda-taljo
Copy link

boda-taljo commented Jul 29, 2015

Finally it worked, thank you for your code, I had extra "s 👯

@shpakovski
Copy link

shpakovski commented Nov 24, 2015

Thank you very much!

@asefnoor
Copy link

asefnoor commented Dec 22, 2015

Thanks. worked like charm.

@Dhavalmaheta
Copy link

Dhavalmaheta commented Mar 23, 2016

Thanks a lot..
its working fine.

@zaczh
Copy link

zaczh commented Apr 17, 2016

very useful!

@tvPM123
Copy link

tvPM123 commented Apr 19, 2016

Hi. Thanks for the example. Works well.
Im facing issues uploading multiple images using the above code. It only accepts the first image. #ios

Please help to execute this code for multiple images

@sweatyc
Copy link

sweatyc commented Jun 23, 2016

What if i want to upload gif (to Giphy.com)?

@mohsinka
Copy link

mohsinka commented Apr 18, 2017

Awesome! So simplistic.

@samuelDeveloperiOS
Copy link

samuelDeveloperiOS commented Sep 26, 2017

Great!

@brunomunizaf
Copy link

brunomunizaf commented Sep 28, 2017

Thank you, you have just saved me from using AFNetworking and/or others. I'm facing an issue now with the HTTP Body - too long when trying to upload a huge gif file. :(

@Rj707
Copy link

Rj707 commented Dec 8, 2017

@Asha149 did you find any solution?

@Rj707
Copy link

Rj707 commented Dec 8, 2017

and can we upload an array of images to server? using NSURLConnection

@Supertecnoboff
Copy link

Supertecnoboff commented Feb 6, 2018

What if you want to post a video using multipart/form-data ?

@erimkurt
Copy link

erimkurt commented May 2, 2018

Thanks man! You saved me...

@krasnorutsky
Copy link

krasnorutsky commented Nov 13, 2018

Thank you! You saved my day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment