如何使用objective-c上传文件到box-api?

时间:2012-12-13 12:47:26

标签: objective-c box-api

我使用AFNetworking进行网络操作

那么向https://api.box.com/2.0/files/content发送请求的正确方法是什么? 现在我收到HTTP // 1.1 500内部服务器错误

代码

NSMutableData *body = [NSMutableData data];
NSString *boundaryString = [NSString stringWithFormat:@"rn-------------------------%.0frn", [[NSDate date] timeIntervalSince1970]];
NSData *boundary = [boundaryString dataUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:kBoxNetFileUploadURLFormat];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[BoxNet attachAuthHeaderForRequest:request];

[request addValue:[BoxNet SHA1:content] forHTTPHeaderField:@"Content-MD5"];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=\"%@\"", boundaryString] forHTTPHeaderField:@"Content-Type"];


[body appendData:boundary];

[body appendData:[@"Content-Disposition: form-data; name=\folder_id\"" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[folderID dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:boundary];

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; filename=\"%@\"; name=\"filename\"", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:content];

[body appendData:boundary];

[request setHTTPBody:body];
[request setValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField:@"Content-Length"];


AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    completion(JSON, nil);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *err, id JSON) {
    completion(JSON, err);
}];

[operation start];

2 个答案:

答案 0 :(得分:1)

问题是Wrong boundary & parameters formatting,请求处理是一样的。

附加新代码:

NSMutableData *body = [NSMutableData data];
NSString *boundary = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];

NSURL *url = [NSURL URLWithString:@"https://api.box.com/2.0/files/content"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[BoxNet attachAuthHeaderForRequest:request];

[request addValue:[BoxNet SHA1:content] forHTTPHeaderField:@"Content-MD5"];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];

// add file body and filename
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data;filename=\"%@\";name=\"filename\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type:application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:content];

// folder id
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition:form-data;name=\"folder_id\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[folderID dataUsingEncoding:NSASCIIStringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

由于这篇文章我已经解决了我的问题:Objective-C Box 2.0 File Upload - Problems

答案 1 :(得分:0)

我个人更喜欢ASIHTTPRequest上传文件和POST表格数据,这对用户来说非常好,这里是code sample.