问题
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.
回答1:
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. :-)
来源:https://stackoverflow.com/questions/11564069/objective-c-box-2-0-file-upload-problems