iOS-使用 REST API 上传图片到 OneDrive

标签 ios rest onedrive

我正在尝试使用他们提供的 REST API 将图像从我的应用程序上传到 onedrive。 但是缺少一些PST方法的格式,请帮忙解决。 我正在做,

NSString *urlString = [NSString stringWithFormat:@"https://apis.live.net/v5.0/me/skydrive/files?access_token=%@",oneDriveAccessToken];
NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

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

//-- Append data into post url using following method
NSMutableData *body = [NSMutableData data];

//-- "image_name" is file name of the image (we can set custom name)
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

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


//-- Sending data into server through URL
[request setHTTPBody:body];

//-- Getting response form server
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//-- JSON Parsing with response data
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Result = %@",result);

我得到的回应是:

Result = {
    error =     {
        code = "request_body_invalid";
        message = "The request entity body for multipart form-data POST isn't valid. The expected format is:
\n--[boundary]
\nContent-Disposition: form-data; name=\"file\"; filename=\"[FileName]\"
\nContent-Type: application/octet-stream
\n[CR][LF]
\n[file contents]
\n--[boundary]--[CR][LF]";
    };
}

我正在关注这个 http://msdn.microsoft.com/en-us/library/hh826531.aspx#uploading_files

根据 Jeffery 的建议,错误信息为,

Result = { error = { code = "request_body_invalid"; message = "The 
request entity body has an incorrect value in the 'Content-Disposition' 
header. The expected format for this value is 'Content-Disposition: form-
data; name=\"file\"; filename=\"[FileName]\"'."; }; } 

最佳答案

作为一种猜测,您正在破坏行尾。有时您使用 \n 有时使用 \r\n。您必须始终使用 \r\n。作为旁注,您不应该在第一次 -appendData 调用时需要初始 \r\n 。最后,检查您的边界字符串:它们通常较长以避免内容中有完全相同的字节。

NSString *boundary = @"A300x-make-it-longer-to-reduce-risk-12345";

…

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

[body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"file\"; filename=\"%@\"\r\n", @"name.JPG"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];

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

关于iOS-使用 REST API 上传图片到 OneDrive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28043963/

相关文章:

iphone - 将 URL(作为字符串)添加到数组时出现问题

java - WebTarget 和线程安全

ios - 在 xCode 中导入用 <> 符号包围的 .h 文件?

用于连接到 One Drive for Business 的 Python 脚本

ios - 播放音乐中的所有歌曲

ios - uidatepicker 总是滚动到当前时间

java - Spring MVC 图片上传使用 Mockmvc 和 JSON 集成测试失败

java - 如何处理 CXF restful webservices 中的可选参数

azure - Onedrive for Business 的 OAuth 流程

ios - 背景音频无法与Xcode 7一起使用