Objective-C Box 2.0 File Upload - Problems - objective-c

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

Related

Xcode Upload File (Image) to FTP site

i want to upload a file, an image, to a website. I have read the SimpleFTPSample from Apple. It´s work with the TestPhotos but it´s too complicated. Can someone post a simple Code, where i can put my URL in it? I have used BlackRaccoon and ASIHTTPRequest but it doesn´t work.
Thanks
What worked easiest for me was to write a small php script and upload the file to that. The php isn't that sophisticated but it works
be sure to replace the urls with your site name
upload.php
<?php
$uploaddir = 'Images/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "transfer successful at: http://YOURWEBSITE.COM/{$uploaddir}/{$file}";
}
?>
obj-c
NSString *imageString = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#\"\r\n", YOURIMAGE];
NSData *imageData = UIImageJPEGRepresentation(newimageGlobal, 0.4);
NSString *urlString = #"http://YOURWEBSITE.COM/upload.php";
NSLog(#"upload url%#", urlString);
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",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:imageString ] 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];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSURLRequest properly format multipart/form-data

I've created an NSURLRequest that's firing off requests OK. I've got it wired to requestbin, but instead of requestbin parsing the pertinent form data, it's seeing it as raw body.
I've got a working CURL command that does this correctly, and as far as I can tell both request raw bodies look the same -- I can't figure out what in the Obj-c I need to change to get those form parameters to work properly.
Here's the inspect link for the requestbin: http://requestb.in/1iszums1?inspect
Working CURL:
curl -X POST -H "Authorization: Bearer token" -H "X-ADN-Pretty-JSON: 1" -F "type=com.example.test" -F "content=#/Users/me/Desktop/test.jpg" "http://re
questb.in/1iszums1"
Obj-c
NSString* url = #"http://requestb.in/1iszums1";
UIImage* img = [UIImage imageNamed:#"test.jpg"];
NSData *imageData = UIImageJPEGRepresentation(img, 1.0f);
NSString* token = #"token";
NSString* name = #"test.jpg";
NSString* type = #"com.example.test";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"------------------------------6c491b1c62c8";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
[request setValue:[NSString stringWithFormat:#"Bearer %#", token] forHTTPHeaderField:#"Authorization"];
[request setValue:#"1" forHTTPHeaderField: #"X-ADN-Pretty-JSON"];
// 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=\"type\"\r\n\r\n%#\r\n", type] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"content\"; filename=\"%#\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
if (imageData) {
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(#"Error: %#", error);
} else {
NSString *responseBody = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"%# %#", response, responseBody);
}
}];
Not having tried it, my guess is that you are missing a couple of dashes on your boundary:
change
[body appendData:[[NSString stringWithFormat:#"%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
to
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
Its tough to keep all of the dashes figured out. If your boundary string as declared in the content type is "foo" then you will need to separate each part with "--foo" and end the whole form data with "--foo--"

Trying To Send Multipart/Form Data in Objective-C, To µTorrent's Web API

I've been trying to get this code working to send a torrent's data to the µTorrent Web API, unfortunately, as far as I can tell, nobody has ever tried to make a .torrent file handler for µTorrent in Objective-C before, which isn't all that surprising.
I have this code here at the moment, but I invariably get the error message: "Error: torrent file content not supplied in form parameter".
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#?token=%#&action=add-file", [self getBaseURL], token]]];
NSString * boundary = #"!#!#!#!#!#!#!#!#!";
NSMutableData * body = [NSMutableData alloc];
NSData * torrentFileContents = [[NSFileManager defaultManager] contentsAtPath:fileName];
[request setHTTPMethod:#"POST"];
[request setValue:[NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary] forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary] forHTTPHeaderField:#"enctype"];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: %#\r\n\r\n", #"application/x-bittorrent"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"torrent_file\"; filename=\"%#\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Length: %ld\r\n", [torrentFileContents length]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:torrentFileContents]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
Why are you putting the content type and length in the request body? Did you try using setValue to add the necessary HTTP headers like Content-Type?
for example:
[request addValue:#"text/xml;charset=utf-8" forHTTPHeaderField:#"Content-Type"];
I found an example of what I wanted to do, written in JS, I was able to make it work by tweaking the code to look like this.
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#?token=%#&action=add-file", [self getBaseURL], token]]];
NSString * boundary = [NSString stringWithFormat:#"AJAX-----------------------%f", [[[NSDate alloc] init] timeIntervalSince1970]];
NSMutableData * body = [NSMutableData alloc];
NSData * torrentFileContents = [[NSFileManager defaultManager] contentsAtPath:fileName];
[request setHTTPMethod:#"POST"];
[request addValue:[NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary] forHTTPHeaderField:#"Content-Type"];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"torrent_file\"; filename=\"%#\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: %#\r\n\r\n", #"application/x-bittorrent"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:torrentFileContents]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
return request;

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

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.