盒子文件上传api从iOS是行不通的 [英] box file upload api from iOS is not working

查看:179
本文介绍了盒子文件上传api从iOS是行不通的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用框的HTTP API(不是iOS SDK)将文件上传到box帐户,但由于某种原因,它不能正常工作并返回状态码400.以下代码中的错误是什么?

  NSString * uploadURL = @https://upload.box.com/api/2.0/files/content; 
NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession * upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:uploadURL]];
[request setHTTPMethod:@POST];

[请求addValue:[NSString stringWithFormat:@Bearer%@,@box_access_token_here] forHTTPHeaderField:@Authorization];

NSString * boundary = @some_random_boundary_value;
NSString * contentType = [NSString stringWithFormat:@multipart / form-data; boundary =%@,boundary];
[request addValue:contentType forHTTPHeaderField:@Content-Type];
NSMutableData * body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@ - %@ \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@Content-Disposition:form-data; name = \attributes\\r\\\
\r\\\
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@Content-Type:application / json\r\\\
\r\\\
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@'{\name \:\%@ \,\parent \:{\id \ :\%@ \}}',@file_name,@box_folder_id] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@\r\\\
- %@ \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@Content-Disposition:form-data; name = \file \\r\\\
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@Content-Type:%@ \r\\\
\r\\\
,@mime_type] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:UIImageJPEGRepresentation(someUIImage,1.0)];

[body appendData:[[NSString stringWithFormat:@\r\\\
- %@ - \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:[NSString stringWithFormat:@%lu,(unsigned long)body.length] forHTTPHeaderField:@Content-Length];
[request setHTTPBody:body];

NSURLSessionUploadTask * uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:body completionHandler:^(NSData * data,NSURLResponse * response,NSError * error){
if(error){
NSLog(@%@,error.debugDescription);
}其他{
NSLog(@%@,@成功上传);
}
}];

[uploadTask resume];


解决方案

我不确定代码有什么问题,当试图按照这里描述的方式实现上传时,我得到了同样的 400 Bad Request https://box-content.readme.io/#upload-a-file



我最终复制了Android示例代码使用的行为 - 它与API文档中的行为有些不同:

  Content-Type:多部分/格式的数据; border = dcf6d5da-6b79-4d80-8503-9ca17037e7c3 
内容长度:757
--dcf6d5da-6b79-4d80-8503-9ca17037e7c3
内容处置:form-data; name =parent_id
Content-Type:text / plain; charset = UTF-8
Content-Length:10
Content-Transfer-Encoding:binary

3973764013
--dcf6d5da-6b79-4d80-8503-9ca17037e7c3
内容处理:表单数据; NAME = 文件名;文件名=你的文件名
Content-Type:text / html
Content-Length:300
Content-Transfer-Encoding:binary

文件二进制内容到这里
--dcf6d5da-6b79-4d80-8503-9ca17037e7c3--

请注意如何使用 parent_id 文件名部分 - 这与API文档中的意见完全不同。 / p>

I am trying to upload a file to a box account using box's HTTP API (not iOS SDK), but for some reason its not working and returning status code 400. What is my mistake in the following code?

NSString *uploadURL = @"https://upload.box.com/api/2.0/files/content";
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:uploadURL]];
[request setHTTPMethod:@"POST"];

[request addValue:[NSString stringWithFormat:@"Bearer %@", @"box_access_token_here"] forHTTPHeaderField:@"Authorization"];

NSString *boundary = @"some_random_boundary_value";
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", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"attributes\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/json\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"'{\"name\":\"%@\", \"parent\":{\"id\":\"%@\"}}'", @"file_name", @"box_folder_id"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"file\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", @"mime_type"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:UIImageJPEGRepresentation(someUIImage, 1.0)];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)body.length] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:body];

NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (error) {
        NSLog(@"%@", error.debugDescription);
    } else {
        NSLog(@"%@", @"Successfully uploaded");
    }
}];

[uploadTask resume];

解决方案

I'm not sure what is wrong with code but I was getting the same 400 Bad Request when tried to implement upload in the way it's described here https://box-content.readme.io/#upload-a-file.

I ended up replicating behavior that Android sample code uses - and it's somewhat different from what is in API docs:

Content-Type: multipart/form-data; boundary=dcf6d5da-6b79-4d80-8503-9ca17037e7c3
Content-Length: 757
--dcf6d5da-6b79-4d80-8503-9ca17037e7c3
Content-Disposition: form-data; name="parent_id"
Content-Type: text/plain; charset=UTF-8
Content-Length: 10
Content-Transfer-Encoding: binary

3973764013
--dcf6d5da-6b79-4d80-8503-9ca17037e7c3
Content-Disposition: form-data; name="filename"; filename="your-file-name"
Content-Type: text/html
Content-Length: 300
Content-Transfer-Encoding: binary

your file binary content goes here
--dcf6d5da-6b79-4d80-8503-9ca17037e7c3--

Please note how parent_id and filename parts are used - this is quite in contrast to what is advices in API docs.

这篇关于盒子文件上传api从iOS是行不通的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆