How to do specific HTTP POST in objective c (working curl line included)?
My API needs to receive some parameters and image via HTTP POST. In curl it is done with:
curl http://someurl -F 'service[lat]=12.22' -F 'sevice[lng]=12.33' -F 'service[user_id]=4' -F 'service[location]=Vienna' -F 'service[image]=@tour.jpg'
How can it be done in objective C (iphone)? I guess it be done with content type set to "application/x-www-form-urlencoded"?
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"random string of your choosing";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
// file
[body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="name"; filename="%@"rn", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
// length
[body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="size"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%i",imageData.length] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
// lat
[body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="lat"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%f",lat] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
// lon
[body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="lon"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%f",lon] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
// location
[body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="location"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:location] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
// user_id
[body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="user_id"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:user_id] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//bon voyage
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
I suggest you take a look at the ASIHTTPRequest framework if you want to avoid the hassle of building the post data yourself.
Example from documentation :
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];
[request addFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photos"];
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];
I use it in all my projects, it's very robust and easy to use.
Please do NOT use ASIHTTPRequest. From the developer's homepage:
Please note that I am no longer working on this library - you may want to consider using something else for new projects. :)
I've heard good things about AFNetworking
But implementing POST Requests via NSURLRequest
is not that hard and you do not depend on another library.