OCRApiService.com SDK issue? - ios

I have a question regarding to the code that OCRApiService.com provides to devs.
This is the code they provide:
//author : Altaf A. M.
//Code Snippet to convert the captured/picker'd (sic) image via OCRApiService
//Input parameter: UIImage
//output parameter: NSString
//updates possible(rather necessary): integrate it asynchronously
- (NSString *) upload:(UIImage *)myImage
{
NSString *urlString = #"http://api.ocrapiservice.com/1.0/rest/ocr";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
// file
// NSData *imageData = UIImagePNGRepresentation(myImage);
NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);
[body appendData:[[NSString stringWithFormat:#"--%#", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: attachment; name=\"image\"; filename=\".jpg\""] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithString:#""] dataUsingEncoding:NSUTF8StringEncoding]];
// Text parameter1
NSString *param1 = #"en";
[body appendData:[[NSString stringWithFormat:#"--%#", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"language\""] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#""] dataUsingEncoding:NSUTF8StringEncoding]];
// Another text parameter
NSString *param2 = #"YOUR_API_KEY";
[body appendData:[[NSString stringWithFormat:#"--%#", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"apikey\""] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param2] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#""] dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:#"--%#--", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnedString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
// NSLog(#"received string = %#", returnString);
// UIAlertView *returnedText = [[UIAlertView alloc] initWithTitle:#"Returned Text" message:[NSString stringWithFormat:#"%#", returnString] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
// [returnedText show ];
// [returnedText release];
return returnedString;
}
I put in my API Key but the problem is whenever I call this method, I get
received string = HTTP/1.1 400 Bad Request
The language parameter is missing
According to the docs, I have set the language correctly but I am not sure why it is still coming up with this error message...
Thanks!

I am the owner of ocrapiservice.com.
The api is working but there is a bug in the code snippet.
Could you please open an issue on https://github.com/smart-mobile-software/ocrapiservice/issues ?
We will correct it within the week.

Related

Posting data with image to server in iOS application

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!

Error while uploading file

I m uploading image using post method but every time i find following error:
The operation couldn’t be completed. (NSURLErrorDomain error -1012.)
Please suggest me the way by which i can resolve this issue:(
NSString *urlString = [NSString stringWithFormat:#"%#%#",Base_URL,URL_Upload];
NSURL* requestURL = [NSURL URLWithString:urlString];
NSString *filename = imageName;
//creating request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:#"POST"];
// set URL
[request setURL:requestURL];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *postbody = [NSMutableData data];
NSData* imageData = UIImageJPEGRepresentation(image, 1);
if (imageData) {
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString* fileData = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"file\"; filename=\"%#\"\r\n", filename];
NSLog(#"fileData %#",fileData);
[postbody appendData:[fileData dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:imageData];
[postbody appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:postbody];
// set the content-length
NSString *postLength = [NSString stringWithFormat:#"%d", [postbody length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[_connection start];
if (_connection) {
_reposnseData = [NSMutableData data];
}
NSString *str=[NSString stringWithFormat:#"%#registration.php",appdel.baseUrl];
NSString *urlString = [NSString stringWithFormat:#"%#",str];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = #"---------------------------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:[#"Content-Disposition: form-data; name=\"file\"; filename=\"a.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imgData]];
[body appendData:[#"\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.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter token
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"email\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[eMail.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter method
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"pass\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[passWord.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter method
NSLog(#"%#",countryCode);
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"country\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[countryCode dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\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];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
NSString *urlString = [NSString stringWithFormat:#"http://********/web-services/register_user.php?firstname=%#&lastname=%#&email=%#&password=%#&location=india&device=IPHONE",details.fname,details.lname,details.emailAddress,details.password];
// urlString=[urlString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
UIImage *image=details.pic;
NSData *imageData =UIImageJPEGRepresentation(image, 0.1);
double my_time = [[NSDate date] timeIntervalSince1970];
NSString *imageName = [NSString stringWithFormat:#"%d",(int)(my_time)];
NSString *string = [NSString stringWithFormat:#"%#%#%#", #"Content-Disposition: form-data; name=\"profile_pic\"; filename=\"", imageName, #".jpg\"\r\n\""];
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 stringWithString:string] 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]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString*s11= [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *responseDictionary1;
responseDictionary1 = [XMLReader dictionaryForXMLString:s11 error:nil];

Sending a UIImage and text to server

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 :)

Attaching an image and posting to a JSON API

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!

Multipart/Formdata image upload, get the file

in my app i want to upload an image chosen via UIImagePickerController to a database which only accepts JPEG images. I've browsed many questions in here, and in other forums but i still didn't get it to work. I hope you can check my code, if there is a mistake which i can't see.
This is the complete method for uploading, with an image description, an image title, and the geodata for this image:
- (void) uploadPic{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *destinationFilePath = [[NSString alloc] initWithFormat: #"%#/%#.jpeg", documentsDirectory, self.chosenImage.imgTitle];
NSURL *fileURL = [[NSURL alloc]initWithString:destinationFilePath];
NSLog(#"will upload file: %#", destinationFilePath);
NSData *imageURLdata = [[NSData alloc]initWithContentsOfURL:fileURL]; (*1)
NSData *imageData = UIImageJPEGRepresentation(self.chosenImage, 90); (*2)
//Here i tried 2 ways of getting the data for uploading, but both don't work.
// create the URL
NSURL *postURL = [NSURL URLWithString:#"http://*********/PictureUpload"];
// create the connection
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
// change type to POST (default is GET)
[postRequest setHTTPMethod:#"POST"];
// just some random text that will never occur in the body
NSString *stringBoundary = #"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value, user session ID added
NSString *headerBoundary = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",
sessionID];
// set header
[postRequest addValue:headerBoundary forHTTPHeaderField:#"Content-Type"];
// create data
NSMutableData *postBody = [NSMutableData data];
// title part
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.imgTitle dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// desc part
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"desc\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.imgDescription dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// latitude part
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"latitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.latitude dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// longitude part
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"longitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.longitude dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// media part
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"file\"; filename=\"%#.jpeg\"\r\n", self.chosenImage.imgTitle ] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:imageURLdata]];
[postBody appendData:[#"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary
[postBody appendData:[[NSString stringWithFormat:#"--%#--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *s = [[NSString alloc] initWithData:postBody encoding:NSASCIIStringEncoding];
NSLog(#"%#", s);
// add body to post
[postRequest setHTTPBody:postBody];
// pointers to some necessary objects
NSURLResponse* response;
NSError* error;
// synchronous filling of data from HTTP POST response
NSData *responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
if (error)
{
NSLog(#"Error: %#", [error localizedDescription]);
}
// convert data into string
NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes]
length:[responseData length]
encoding:NSUTF8StringEncoding];
// see if we get a welcome result
NSLog(#"%#", responseString);
[self responseHandler:responseString];
}
The GEOimage chosenImage is created via the CGImageRef after an UIImage is chosen in the ImagePickerController.
Method nr. *1 to get the NSData for upload is not the best, because here the image is chosen from the document directory, and here every EXIF information about the image is deleted.
With both methods i get the response from the database that the image filetype is not supported (JPEG expected).
I thought with method nr. *2 i'm sending an JPEG image, but perhaps i have a mistake in the whole multipart/formdata process.
I tried to get the URL to the original file on the filesystem, but this is quite difficult for me. I only got the assets-url from the image.
Thanks in advance
S4lfish
You've probably solved it on your own by now, but I think I see your problem.
NSString *stringBoundary = #"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value, user session ID added
NSString *headerBoundary = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",
sessionID];
When you're defining the boundary inside the MIME header, you're using your sessionID as the boundary. Then, down below, you're using your stringBoundary variable to create the actual boundaries.
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
It might also be a good idea to provide the Content-Length of each MIME block to help the processing code, but I don't think that's at issue here.
// Try this ---->>>
NSMutableDictionary *post_dic=[[NSMutableDictionary
alloc]initWithCapacity:20]; [post_dic setObject:firstName_txtFld.text
forKey:#"firstname"]; [post_dic setObject:lastName_txtFld.text
forKey:#"lastname"]; [post_dic setObject:email_txtFld.text
forKey:#"email"]; [post_dic setObject:password_txtFld.text
forKey:#"password"]; [post_dic setObject:country_txtFld.text
forKey:#"address"]; [post_dic setObject:state_txtFld.text
forKey:#"state"]; [post_dic setObject:city_txtFld.text
forKey:#"city"]; [post_dic setObject:zip_txtFld.text forKey:#"zip"];
[post_dic setObject:phoneNumber_txtFld.text forKey:#"phonenumber"];
NSURL *url = [NSURL URLWithString:#"Enter your url"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest
requestWithURL:url]; [urlRequest setHTTPMethod:#"POST"]; [urlRequest
setHTTPBody:[urlString dataUsingEncoding:NSUTF8StringEncoding]];
NSString *boundary = #"--------------------------14737809831466499882746641449"; NSString
*contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[urlRequest addValue:contentType
forHTTPHeaderField: #"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
for (NSString *param in post_dic) {
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data;
name=\"%#\"\r\n\r\n", param]
dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"%#\r\n", [post_dic objectForKey:param]]
dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data;
name=\"filedata\";
filename=\"%#.jpg\"\r\n",#"text"]
dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[#"Content-Type: application/octet-stream\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
//NSLog(#" image -->%#",imageData);
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:postbody]; [urlRequest setURL:url];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
[connection start];

Resources