Failing to POST NSData of PDF to server - cocoa-touch

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.

Related

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.

Objective-C Box 2.0 File Upload - Problems

I've been trying to get file upload working with Box for the past few days. I know I'm doing something wrong, but just can't see what it is.
I've reduced my code down as much as I possibly can, to just this:
// Configure the request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"https://api.box.com/2.0/files/data"]];
[request setHTTPMethod:#"POST"];
[request setValue:boxAuthString forHTTPHeaderField:#"Authorization"];
// Setu up the request
NSString *boundary = [NSString stringWithString:#"--PLEASEHELPMEGETTHISWORKING"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#\r\n",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
// Add the info and data for the file.
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition:form-data;name=\"filename\";filename=\"testfile.txt\"\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type:text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Hello"] dataUsingEncoding:NSUTF8StringEncoding]];
// Add the info and data for the target folder.
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition:form-data;name=\"folder_id\"\r\n\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"0"] dataUsingEncoding:NSASCIIStringEncoding]];
// Close the body and set to the request
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", boundary] dataUsingEncoding: NSUTF8StringEncoding]];
[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);
But, I still get a return of:
{"type":"error","status":404,"code":"not_found","help_url":"http://developers.box.com/docs/#errors","message":"Not Found","request_id":"10130600215xxxxxxxxxxx"}
Please can someone help point me in the right direction, as when I use POSTMAN or cURL, I can get it to work - so it is obviously an issue with my code. I suspect it is something to do with the 'folder_id', but can't seem to diagnose the actual cause.
Fixed it. The problem was in my 'boundary' construction code. I'd got:
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#\r\n",boundary];
And should have had:
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
Spot the incorrect termination '\r\n'. Oh well. Only wasted about 4 hours on this. :-)

Printing to Google Cloud Print using iOS

I have been trying to print a PDF file to Google cloud print using the following code. Instead of actually printing the document, it is only printing a string that shows the location of the file as well as the file name. I would like to have it print the actual document.
We can assume that the data being passed into this method is valid as it seems to be finding the correct printer from calls to cloud prints /search
Update: The code is updated from its original format to include some multi-part form data
NSString *currentPDFFileName = [[NSBundle mainBundle] pathForResource:#"TestPDF" ofType:#"pdf"];
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 *thePrinter = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"printerid\"\r\n\r\n%#", printerID];
NSString *pdf = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"content\"; filename=\"%#\"\r\n", [currentPDFFileName lastPathComponent]];
NSString *theContent = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"contentType\"\r\n\r\n%#", contentType];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[thePrinter dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[pdf dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[theContent dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/pdf\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:pdfData];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *pdfRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"https://www.google.com/cloudprint/submit"]];
[pdfRequest setValue:#"multipart/form-data; boundary=----foo" forHTTPHeaderField:#"Content-type"];
[pdfRequest setHTTPMethod:#"POST"];
[pdfRequest setHTTPBody:body];
[self.authenticate authorizeRequest:pdfRequest completionHandler:^(NSError *error) {
NSString *output = nil;
if(error) {
output = [error description];
} else {
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:pdfRequest
returningResponse:&response
error:&error];
if(data) {
output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
} else {
output = [error description];
}
}
NSLog(#"%#", output);
}];

Best practice when using similar code in methods

I have a Utility method to comunicate with an API, it communicates using a POST HTTP request, in my utility-class i have a method called:
(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters;
wich takes the parameters and sets up the body of the POST. however, in one particular case i want to upload some images and the code is slightly altered to make uploading of said images possible, what is the best practice for this case? should i rename the method to:
(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters andImages(NSArray*)images;
and set nil as a parameter in all other cases, or should i set a bool in the method calling "makeConnectionWithParameters" and check in the method if the bool is set and in that case process the images?
Any other ideas to make the code prettier?
here is the method:
(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:BASE_URL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20];
request.HTTPMethod = #"POST";
NSString *boundary = #"myR4ND0Mboundary";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
//Lägg till inloggningsuppgifter för API-anropet
[parameters setValue:API_LOGIN forKey:#"api-login"];
[parameters setValue:API_PASSWORD forKey:#"api-password"];
//Lägg till alla parameterar i POST-bodyn
NSMutableData *body = [NSMutableData data];
for (NSString *param in parameters)
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if(hasImages)
{
int c = 0;
for(UIImage* image in self.images)
{
c++;
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
if (imageData)
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpg\"\r\n", [NSString stringWithFormat:#"image%d", c]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
imageData = nil;
}
}
//Sätt content-length
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:body];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(connection)
{
receivedData = [NSMutableData dataWithLength:0];
}
}
I hope this all makes sense. :)
Simply create two methods that make the request objects, not the actual connections:
(NSMutableURLRequest*)makeConnectionWithParameters:(NSMutableDictionary*)parameters;
(NSMutableURLRequest*)makeConnectionWithParameters:(NSMutableDictionary*)parameters andImages(NSArray*)images;
Inside the first method call:
[self makeConnectionWithParameters:parameters andImages:nil];
...and have a nil check for the images inside the second method. Use those two methods to get your request then make the NSURLConnection with the returned object.

How to send JSON request to service with parameters in Objective-C

I'm creating a iPhone app, and im trying to figure out how to create a JSON request to the webservice that contains parameters. In Java this would look like this
HashMap<String, String> headers = new HashMap<String, String>();
JSONObject json = new JSONObject();
json.put("nid", null);
json.put("vocab", null);
json.put("inturl", testoverview);
json.put("mail", username); // something#gmail.com
json.put("md5pw", password); // donkeykong
headers.put("X-FBR-App", json.toString());
The header has to contain a JSON object and "X-FBR-App" before the service recognizes the request. How would this be implemented in Objective-C ?
USE Post method.try this
NSString *method = #"addProduct";
NSString *jsonstring=#"http://yourdomain.com/product_review_api/product-review.php?";
NSString *urlString = [NSString stringWithFormat:#"%#",jsonstring];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
// image file
NSData *imageData = UIImageJPEGRepresentation( labelimage, 0.8);
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
// 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]];
// parameter categoryid
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"category_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[selectedID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[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);
NSDictionary *profile = (NSDictionary*)[returnString JSONValue];
NSLog(#"profile=%#", profile);
you can append all the parameters like this.
JSON isn't baked into iOS, but if you look at www.JSON.org there are a number of Objective-C frameworks that provide the required functionality. I have no experience with any of them so I can't make recommendations, but it is a good start to finding a solution.
Also look at this SO question post-data-through-json-webservices-in-iphone