【发布时间】:2012-11-05 11:33:23
【问题描述】:
我一直在尝试使用 Box 2.0 API 将文件从 Objective C 客户端上传到我的 Box 文件夹。我读过一些帖子:
- how to send form data in a programmatic file upload in box api 2.0
- API 2.0 how to upload file with POSTMAN?
我已成功尝试使用 Curl,如文档中所述,但在尝试创建 NSMutableUrlRequest 时总是得到 404。
这是我的代码:
NSURL *URL = [NSURL URLWithString:@"https://api.box.com/2.0/files/content"];
urlRequest = [[NSMutableURLRequest alloc]
initWithURL:URL cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
[urlRequest setHTTPMethod:@"POST"];
AppDelegate *appDelegate = [AppDelegate sharedDelegate];
NSString *p = [NSString stringWithFormat:@"BoxAuth api_key=%@&auth_token=%@",API_KEY,appDelegate.boxAuthToken];
[urlRequest setValue:p forHTTPHeaderField:@"Authorization"];
[urlRequest setValue:@"multipart/form-data, boundary=AaB03x" forHTTPHeaderField:@"Content-Type"];
NSString *postBody = @"--AaB03x"
@"content-disposition: form-data; name=\"filename\"; filename=\"test.txt\";"
@"folder_id=466838434"
@"Content-type: text/plain"
@""
@"testing box api 2.0"
@""
@"--AaB03x--";
NSData *data = [postBody dataUsingEncoding:NSUTF8StringEncoding];
[urlRequest setHTTPBody:data];
[urlRequest setValue:[NSString stringWithFormat:@"%d",[data length]] forHTTPHeaderField:@"Content-Length"];
【问题讨论】:
标签: iphone objective-c ipad box-api