I have a simple question- I'm currently writing a specific part in my app related to sending data to server.
I tried to send text, succeeded. Sent image, succeeded.
What I'm trying to do now is to send them both within one POST request.
I figured out that I need to use something that is called multipart/form-data and boundaries, but I haven't found anymore info about it.
So how can I send both text and image in one, simple POST request? And how can I check for errors during upload, afterwards etc.?
Thanks!
Reference code I've written but sending 0 bytes of info:
NSData *imageData = UIImageJPEGRepresentation(sendImage, 1.0);
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"http://posttestserver.com/post.php?dir=something"]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------54737809831466490885746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"rn--%#rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"userfile\"; filename=\"reportingImage.jpg\"rn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"rn--%#--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: text/xml" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[[alertView textFieldAtIndex:0] text] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
This is a working snippet that sends text and image, optionally a few texts with a few params per each
//After dismissing the alert, we get its text (user location and notes) and the picture he took
NSMutableData *body = [NSMutableData data];
NSURL *url = [NSURL URLWithString:#"http://posttestserver.com/post.php?dir=Doda"]; //Test server, you can access it online to see the upload
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449"; //I have no idea what this is, but without it the code won't work
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[req setValue:contentType forHTTPHeaderField: #"Content-Type"];
//Attaching image
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: attachment; name=\"imageOfReport\"; filename=\"imageOfReport.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:UIImageJPEGRepresentation(sendImage, 1.0)]]; //Crucial, getting a JPEG version of the image and sending it
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"report_description\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[[alertView textFieldAtIndex:0] text] dataUsingEncoding:NSUTF8StringEncoding]]; //Crucial, taking the text from the Alert and sending it
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:body];
//Below are few lines which can add other parameters and text
/* [body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"spid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:body];*/
NSURLConnection *sendingTheData2 = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES]; //Sent! ;)
You probably want to use a network library like AFNetworking, and save yourself some time :)
Related
I am new IOS developer and I am facing a headache issue regarding to use multipart for uploading file and some text, i have tried a lot on here
The status code always return 400. I tired to test my web service with another way, such as build Rest client by HttpComponent API, using RestEASY client and both of them worked successfully, how funny!
I am using Xcode 6.1.1, my source code:
-(void)uploadPhoto {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://localhost/my-rest-service"]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"POST"];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:#"avatar_temp"], 1.0);
NSString *boundary = #"12345-6789-abc"; //
// 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:#"%#--", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=%#\r\n\r\n", #"type"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", #"HELLO"] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[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
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"[String] %#", str);
}
}];
}
Any suggestions will help my investigation
Thanks
An
You forgot to add imageData so add:
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
A full working example:
[request setHTTPMethod:#"POST"];
NSData *imageData = UIImageJPEGRepresentation(imageToSave,0.9); //change imageToSave with your own UIImage defined
NSString *filenames = #"first field";
NSString *token = #"second field";
NSString *boundary = #"---------------------------14738723651466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
////+++ first form field for post -> for example a field called filenames see NSString above
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
////--- the second field for form post-> we named token see above NSString
////+++
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[token dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
////---
////+++ the file used to upload /post
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"ceva.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
////---
// setting the body of the post to the reqeust
[request setHTTPBody:body]; // now lets make the connection to the web
Now all the you want to make is check your php code on server side.
NSString *filenames = [NSString stringWithFormat:#"front_img"];
NSString *urlString = #"http://ccccc.com/upfile.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"filenames1\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames1 dataUsingEncoding:NSUTF8StringEncoding]];
/*Sending First Image start here*/
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#.jpg\"\r\n", filenames]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData1]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
/*Sending First Image ends here*/
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"%#",returnString);
A typical upload http body will look like:
--BOUNDARY_TEXT
Content-Type/Content-Disposition info key values
Base64 encoded file data ...
--BOUNDARY_TEXT
Content-Type/Content-Disposition info key values
Base64 encode file data.
--BOUNDARY_TEXT
You've done the first one, just continue append files according to the format seperatored by the BOUNDARY.
I am successfully posting UIImage to server, but I need to post a NSString with the UIImage.
Below is the code with which I tried to post UIImage to server.
selectedItem is the array where I am storing all selected UIImage from gallery.
The line which I have commented is what I need to send to server.
for (int i=0; i<[selectedItem count]; i++) {
//NSString *post =[[NSString alloc] initWithFormat:#"mid:%#",mid];
NSData *theData = UIImagePNGRepresentation([selectedItem objectAtIndex:i]);
NSString *urlString = #"http://retailertoolkit.com/RTK/upload.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",mid] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"file\"; filename=\"iphoneimage.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:theData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
if(connection) {
} else {
}
}
Every parameter of the request has to be added as you are adding the image to the request.
Try replacing [body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",mid] dataUsingEncoding:NSUTF8StringEncoding]]; with
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"{parameter_name}\";\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[mid dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
Where parameter_name should be your mid string's key (parameter name). Good Luck!
I am using NSURLConnection to integtarte the Web-service.
My code is as follow:
NSURL *url = [NSURL URLWithString:Sign_Up_URL];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString *params = [NSString stringWithFormat:#"vimage=%#&#"vFname=%#&vLname=%#&vEmail=%#&vPassword=%#&vDeviceType=%#&vDeviceToken=%#",image_data,txt_first.text,txt_last.text,txt_email.text,txt_password.text,#"IOS",#"safasf423526dgdsgdsg"];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setValue:header_filed forHTTPHeaderField:#"X_API_KEY"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *connectionError) {
// handle response
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSDictionary *json_response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(#"%#",json_response);
// [self login_response:json_response];
}];
But How can I upload a image with this parameter?
Also Refer this
Help for this.
Please read Article : http://nthn.me/posts/2012/objc-multipart-forms.html, and used ContentType : Multipart/from-data;
for your reference :
NSString *boundary = #"YOUR_BOUNDARY_STRING";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"photo\"; filename=\"%#.jpg\"\r\n", self.message.photoKey] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"message\"\r\n\r\n%#", self.message.message] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"user\"\r\n\r\n%d", 1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
Hope this will help, Please update me if you have getting any error.
By using NSURLConnection you can do like below
NSData* fileData = [[NSData alloc] initWithContentsOfFile:image_data];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:Sign_Up_URL]];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithString:#"0xKhTmLbOuNdArY"]; // This is important! //NSURLConnection is very sensitive to format.
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"VFname\"; filename=\"thefilename\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:fileData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"param2\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:self.emailtextfield.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"param3\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:self.passwordtextfield.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:self.vlnametextfield.text] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
(OR using ASIHTTPRequest you can do it as below)
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:Sign_Up_URL];
[request setDelegate:self];
[request setPostValue:vnamelabel.textforKey:#"VFname"];
[request setPostValue:emaillabel.text forKey:#"vEmail"];
[request setPostValue:passwordlabel.text forKey:#"vPassword"];
[request setPostValue:devicetypelabel.text forKey:#"vDeviceType"];
[request addData:self.imagedata withFileName:#"image.jpeg" andContentType:#"image/jpeg" forKey:#"vimage"];
//----------------------
Hope it helps you..
Hi Follow below links for my answers
Upload image to server with iOS not working
Trouble Passing Variables to PHP Script on Image Upload
How do post Json and Image to server for ios
Send JSON request with iOS
how to GET or POST multipart data request in PHP
PHP to Edit XML, POSTed from iOS
Hope this will help
How can I create a post with an image and attach it as a featured image from iPhone app?
I am using wordpress with JSON API plugin
My url to create post: <-- Reference to this
http://www.example.com/api/create_post/?nonce=eea4bb4ce5&status=publish&title=Test&content=test%20content&categories=animals
Now I need to attach an image from the phone gallery to this url. How can I do this?
Try this. Use JSON POST method. You can append all your parameters like this.
-(void)uploadImage
{
NSString *method = #"";
NSString *username = #"";
NSString *token = #"";
NSString *urlString = [NSString stringWithFormat:#"%#",#"yoururl"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
// file
NSData *imageData = UIImageJPEGRepresentation( imageView.image, 0.8);
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"image\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// parameter username
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[username dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// parameter token
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"auth_token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[token dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// // parameter method
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[method dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"%#",returnString);
[returnString release];
}
Hope this may help you.
To full fill your requirement you need to send image file on web-service using by POST method
First chose image from Photo Library or take it from the camera and make data of that Image using following code.
NSString *imgName = [NSString stringWithFormat:#"ImageTest"];
NSData *imgData=UIImagePNGRepresentation(imgView.image);
NSString* urlstring = [NSString stringWithFormat:#"http://www.example.com/api/create_post/?nonce=eea4bb4ce5&status=publish&title=Test&content=test%20content&categories=animals&imageFile=%#",imgName];
NSLog(#"URL >> %#",urlstring);
NSURL url = [NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//===============================================
NSMutableURLRequest postRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[postRequest setURL:url];
[postRequest setHTTPMethod:#"POST"];
NSString boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[postRequest addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [[NSMutableData alloc] init];
[postRequest addValue:contentType forHTTPHeaderField: #"Content-Type"];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"userfile\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
imgName this is very important object in this code it's image which you need to pass in your web service which is shown as above.
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#.png\"\r\n",imgName]] dataUsingEncoding:NSUTF8StringEncoding]]; //img name
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// Add Image imgData is Declare as Above.
[body appendData:[NSData dataWithData:imgData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:body];
NSURLResponse *response;
NSError error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
NSString *result=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSString * jsonRes = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(#"result = %#",[result JSONValue]);
It perfectly working with me ! I hope you may get help from here!