Post Request with image encoding like data not like string - objective-c

sorry I am new on this.
I have make this to upload a image to a server. The problem is that the guy how has make the server are waiting for a jpg file, and I am sending a jpg encoding in a string. What can I do? All the examples that I have seen send it like in my code.
static NSString *boundary = #"---------------------------14737809831466499882746641449";
UIImage *img = [[UIImage alloc] init];
img = [UIImage imageNamed:#"tarjetaPrueba.jpg"];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(img, 1.0)];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"front\"; \r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:imageData];
[postbody appendData:[[NSString stringWithFormat:#"--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
Thanks.

Related

Http Post Request with Two images files

I have a problem. I need to send 2 jpg files to a server. I have to make a POST request with a body like that:
Body:
front: image file
reverse: image file
how can I make this post request with this two parameters?
I have a try with one parameter:
static NSString *boundary = #"---------------------------14737809831466499882746641449";
UIImage *img = [[UIImage alloc] init];
img = [UIImage imageNamed:#"tarjetaPrueba.jpg"];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(img, 1.0)];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"front\"; \r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:imageData];
[postbody appendData:[[NSString stringWithFormat:#"--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
It is not working...why? I donĀ“t know...
Thanks for the help.
You will have to content-type as multipart/form-data.
Add both images in a dictionary,convert it to NSData and finally append it to request body.
contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
Refer this link for help.
How to send multiple files with post request? (objective-c, iOS)

NSData POST HttpRequest error Objective C

I want to send a document that I obtained as NSData with DocuSign API to a web that has a service to do it.
The service is
POST /api/v1/rest/groups/[:idgroup]/documents/upload/[:oauthtoken]
and the required parameters are:
file --> The file to upload (Type: inputstream)
fileName --> The name of the new file to upload (Type: string)
length --> The size of the file to upload in bytes (Type: long)
I'm trying to do the request with this example Uploading Image via POST in Objective C but I have a service error that says "InvalidParametersServiceApiException"
My code is:
NSString *filename = #"docusignTest.pdf";
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 *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"fileName\"; \r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n%#",filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"length\"; \r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n%lu",(unsigned long)oResponseData.length] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:oResponseData]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
Here's a suggestion: Instead of ignoring the response and the error, ask sendSynchronousRequest to supply them both to you, then examine what you are getting. It's rather mad to send a POST request without any error handling. The data you sent might be wrong, and servers are notorious about wanting you to get it right, but good servers will tell you what is wrong. The server may have problems. The URL may be wrong. The device may not have a connection.
And posting exactly what you received, without any interpretation by you, might help. Reading the documentation for the service you are calling might also help. No two services are the same.
The example of this link is very helpul.
NSString *filename = #"docusignTest.pdf";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[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=\"file\"; filename=\"%#\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:oResponseData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"fileName\"\r\n\r\n%#", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"length\"\r\n\r\n%d", oResponseData.length] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSURLResponse *response;
NSError *error2;
NSData *oResponseData2 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error2];
NSMutableString *jsonResponse2 = [[NSMutableString alloc] initWithData:oResponseData2 encoding:NSUTF8StringEncoding];

Objective C post variables as well as files

I am attempting to write a method/function in objective c to post data to an online php script. All is going well, Its successfully uploads as many files as I provide it with but I cannot figure out to send variables along with these files. I've spent hours now looking through this site and I am unable to find an example that posts files as well as variables.
NSURL * postURL = [NSURL URLWithString:serverURL];
NSString * contentBoundary = #"-14737809831466499882746641449";
NSString * contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", contentBoundary];
// Build Request
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:postURL];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData * postBody = [NSMutableData data];
NSMutableString * postString = [NSMutableString stringWithFormat:#""];
for(NSString * key in dataArray) [postString appendString:[NSString stringWithFormat:#"%#=%#&", key, [dataArray objectForKey:key]]];
[postString deleteCharactersInRange:NSMakeRange([postString length] -1, 1)]; // Trim trailing ampersand from string
NSData * postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
[postBody appendData:[NSData dataWithData:postData]];
if(attachments != nil){
for(NSString * filePath in attachments){
NSString * fileName = [filePath lastPathComponent];
NSData * attachedFile = [NSData dataWithContentsOfFile:filePath];
[postBody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", contentBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"Content-Disposition:form-data;name=\"userfile[]\"; filename=\"%#\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"Content-Type:application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:attachedFile]];
[postBody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", contentBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
NSString * postLength = [NSString stringWithFormat:#"%d", [postBody length]];
[urlRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[urlRequest setHTTPBody:postBody];
Any help would be appreciated.
You can try appending the variable's values to the posted data in the following manner :
// text parameter
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"applicationId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *parameterValue1=[dict objectForKey:#"applicationId"];
[postbody appendData:[parameterValue1 dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
Hope that works for you. Above code is just an example of the parameter sent with the file upload.

Failing to POST NSData of PDF to server

I have an iPad application in which I need to send a server a PDF file, alongside two POST variables, a user ID and a job ID. I am using the code below to do this:
NSData *pdfData = [NSData dataWithContentsOfFile:currentPDFFileName];
NSData *validPDF = [[NSString stringWithString:#"%PDF"] dataUsingEncoding: NSASCIIStringEncoding];
if (!(pdfData && [[pdfData subdataWithRange:NSMakeRange(0, 4)] isEqualToData:validPDF]))
{
NSLog (#"Not valid");
}
NSLog (#"%#", currentPDFFileName);
NSLog (#"%#", [currentPDFFileName lastPathComponent]);
//create the body
NSMutableData *body = [NSMutableData data];
//create the POST vars
NSString *jobID = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"jobid\"\r\n\r\n%#", job.jobID];
NSString *userID = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userid\"\r\n\r\n%#", delegate.userID];
NSString *pdf = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"image_file\"; filename=\"%#\"\r\n", [currentPDFFileName lastPathComponent]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[jobID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[userID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[pdf dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/pdf\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:pdfData];
NSMutableURLRequest *pdfRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com"]];
[pdfRequest setValue:#"multipart/form-data; boundary=----foo" forHTTPHeaderField:#"Content-type"];
[pdfRequest setHTTPMethod:#"POST"];
[pdfRequest setHTTPBody:body];
NSData *pdfDataReply;
NSURLResponse *pdfResponse;
NSError *pdfError;
pdfDataReply = [NSURLConnection sendSynchronousRequest:pdfRequest returningResponse:&pdfResponse error:&pdfError];
NSString *pdfResult = [[NSString alloc] initWithData:pdfDataReply encoding:NSASCIIStringEncoding];
NSLog (#"%#", pdfResult);
The people on the other end of the connection are saying that they are not receiving the PDF file. I have tried all the usual suspects such as trying to find any nil objects.
Would greatly appreciate if anyone could please lend a hand with this!
Thanks.
Ricky.
Turns out there was an issue with the boundaries. I needed to add one at the end, after pdfData was added. Thank you for your help.

Image uploading to the JSP Server

I had a problem with the uploading an image into the server.Also I need to upload a string value to the server with that image. The Image uploading is fine but I am not succeeded so far to upload the string. The following is my code for the uploading. Please help me.Thanq.
NSData *imageData = UIImageJPEGRepresentation(theImage,0.9);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString* width =#"10";
[request setValue:#"300" forHTTPHeaderField:#"Keep-Alive"];
[request setValue:#"keep-live" forHTTPHeaderField:#"Connection"];
[request setValue:width forHTTPHeaderField:#"spotfk"];
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",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.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 stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
Spots* spotObj=[spotsList objectAtIndex:spotIdValue-1];
SpotItems* spotItemObj=[[SpotItems alloc]init];
spotItemObj.imageX_Coordinate=0.0;
spotItemObj.imageY_Coordinate=0.0;
spotItemObj.imageWidth=spotObj.spotWidth;
spotItemObj.imageHight=spotObj.spotHight;
spotItemObj.imageName=#"logo.png";
UIView* subView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, spotObj.spotWidth, spotObj.spotHight)];
subView.backgroundColor=[UIColor clearColor];
spotItemObj.subView=subView;
UIImageView* newSpotItemImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, spotObj.spotWidth, spotObj.spotHight)];
newSpotItemImageView.image=[UIImage imageNamed:spotItemObj.imageName];
[spotItemObj.subView addSubview:newSpotItemImageView];
[spotObj.spotsItemsList addObject:spotItemObj];
[spotItemObj release];
if([spotObj.spotsItemsList count]==2)
{
[self startInternalAnimation:spotObj.spotId-1];
}
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
I got the solution,
That is we need to make the code as follows. The proble is not using dataUsingEndcoding method and boundaries now the code is as follows.
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"spotfk\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:spotFk] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"media\"; filename=\"test.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];