my code
NSString *urlString = [NSString stringWithFormat:#"%#%#", urlHeader, urlBody];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:0 timeoutInterval:TIMEOUT_INTERVAL];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"--Kqcekgk5QEa0NR71B";
NSMutableData *body = [NSMutableData data];
NSMutableData *imageBody = [NSMutableData data];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n%#", BOARD_ID, boardType] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n%d", BOARD_SEQ, [boardSeq intValue]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
for(int i=0; i<[imagesArr count]; i++) {
if([[imagesArr objectAtIndex:i] isMemberOfClass:[UIImage class]]) {
NSData *imageData = UIImageJPEGRepresentation([imagesArr objectAtIndex:i] , 90);
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"FILE_UPLOAD%d\"; filename=\"IMG_IPHONE%d.jpg\"\r\n\r\n", i+1, i+1] 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:imageBody];
send to server success but image file type as text/plan
i use image .png
why image file broken ?
You should remove \r\n at the beginning like this
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
Related
how to change the name of the image??Actually I want to append the phone number from user at image name to make a unique name for image
below is my action button code
NSString *filename =[NSString stringWithFormat:#"%#cache",self.tmpp1];
NSData *imageData = UIImageJPEGRepresentation(_profilei.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", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", #"image"] 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]];
}
// parameter username
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"customer_name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"%#",_nametf.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
the phone number is store in the string mobilestrp and I want to append coz my server accept is if it has the number.Thanks in advance for any suggestions!!
NSString *urlString = #"http://example.com/avatar";
NSLog(#"%#",urlString);
NSString *imageName = #"hello";
NSString *string = [NSString stringWithFormat:#"%#%#%#", #"Content-Disposition: form-data; name=\"avatar\"; filename=\"", imageName, #".jpg\"\r\n\""];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449MJ";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data"];
[request addValue:contentType forHTTPHeaderField: #"Content-type"];
NSString *contentType1 = [NSString stringWithFormat:#"%#",customer_api_key];
[request addValue:contentType1 forHTTPHeaderField:#"Authorization"];
NSString *contentType2 = [NSString stringWithFormat:#"app-customer"];
[request addValue:contentType2 forHTTPHeaderField:#"Requester"];
UIImage *image=DPView.image;
NSData *imageData =UIImageJPEGRepresentation(image, 1);
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\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=\"avatar_name\"\r\n\r\n%#", NameTF.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"avatar_mime_type\"\r\n\r\nimage/jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"customer_id\"\r\n\r\n%d",customer_id] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSLog(#"Body = %#",body);
NSString *bodydecoded = [[NSString alloc] initWithData:body encoding:NSASCIIStringEncoding];
NSLog(#"Bodydecoded = %#",bodydecoded);
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(#"ReturnData = %#",returnData);
if (returnData) {
NSString *s11= [[NSString alloc] initWithData:returnData encoding:NSISOLatin1StringEncoding];
// NSDictionary *responseDictionary1= (NSDictionary *)s11;
NSLog(#"S11 = %#",s11);
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [s11 dataUsingEncoding:NSASCIIStringEncoding]
options: NSJSONReadingMutableContainers
error: nil];
NSLog(#"json finally= %#",JSON);
hello everyone, I am new to IOS so please spare me.
Here I am trying to send Image with 3 header for authorization and 3 contents in body for image related info. But as I append the imagedata and then decode the body, I get this output :
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar"; filename="hello.jpg"
"Content-Type: image/jpeg
ÿØÿà
rest of the content after image is not created or what?
but when do not append the imagedata, i get the whole form-data created normally as :
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar"; filename="hello.jpg"
"Content-Type: image/jpeg
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar_name"
fgh
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar_mime_type"
image/jpg
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="customer_id"
xyz
-----------------------------14737809831466499882746641449MJ
and when i pass this data(in both cases), i get invalid data error from server.(obviously)
please help me, I am stuck.
also the headers I am passing is without boundary = %#,boundary, is that correct. I removed it because I was getting invalid api key error from server.(off-course the authorization error)
Thanks in advance.
please use this for creating multipart form requset
NSString *boundary = [NSString stringWithFormat:#"Boundary-%#", [[NSUUID UUID] UUIDString]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
don't add header in body add as follows
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
create body ad follows
NSMutableData *body = [NSMutableData data];
[dicParams enumerateKeysAndObjectsUsingBlock:^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
}];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"image\"; filename=\"%#\"\r\n", strName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: %#\r\n\r\n", strMime] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:dataImage];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
request is created and use as you want
===================
when you are adding parameters you are doing like following
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"avatar_name\"\r\n\r\n%#", NameTF.text] dataUsingEncoding:NSUTF8StringEncoding]];
"\r\n" is the end tag form which one field is completed.
your first parameter will start with #"\r\n--%#\r\n" which is wrong. this means one field ended and another field is started instead of that you have to do as mentioned below.
add boundary like this
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
add field name like this
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
add field value like this
[body appendData:[[NSString stringWithFormat:#"%#\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
you will get success.
The below is my method to upload a photo to the server. There are two parameters: DeviceToken and ContentModel.ContentModel that contains a dictionary. Inside 2 field there is AlbumID and ContentPath.
NSURL *rtfUrl = [[NSBundle mainBundle] URLForResource:#"AlertBody" withExtension:#".png"];
NSString *deviceToken = #"5FF2C5A6-3930-4102-99A7-A55107B4375C";
NSString *loStr = [NSString stringWithFormat:myUrl];
loStr = [loStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:loStr]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"deviceToken"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:deviceToken] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *contentModel = #{
#"AlbumID" : #"0",
#"contentPath" : [rtfUrl absoluteString],
};
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"contentModel"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"%#",contentModel] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//Adding Image
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:#"AlertBody.png"], 0.3);
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"myfile\"; filename=\"%#\"\r\n",#"AlertBody.png"] dataUsingEncoding:NSUTF8StringEncoding]]; // filetype=\"image/png\";
[postbody appendData:[#"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Close form
[postbody appendData:[[NSString stringWithFormat:#"--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//send data to server
[request setHTTPBody:postbody];
NSString *msgLength = [NSString stringWithFormat:#"%d", [postbody length]];
[request addValue: msgLength forHTTPHeaderField:#"Content-Length"];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[NSString alloc] initWithData:responseData1 encoding:NSUTF8StringEncoding];
NSLog(#"response string ==%#;",responseString);
This is my code. What is the error in this? I am getting this response: Source code **500** .web service is created using ASP.net.
Please Help me. I am facing this problem a lot.
Can you try below code? It's tested and working fine. Hope it may help you to resolve your issue too.
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
[postData appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"yourImage.jpg\"\r\n", #"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:imageData];
[postData appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
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!
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!