使用新的Copy.com API。已成功使用OAuth1登录并已发出配置文件数据请求,并可成功创建文件夹。
如果你在这里查看API文档:https://www.copy.com/developer/documentation#api-calls/filesystem并转到有关向API发出POST
请求以创建新文件的部分,标题的说明对我来说有点奇怪。见这里:
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary5dcD4Bk7SevSsaMg
Content-Disposition: form-data; name="X-Api-Version"
1.0
------WebKitFormBoundary5dcD4Bk7SevSsaMg
Content-Disposition: form-data; name="file"; filename="animation.gif"
Content-Type: image/gif
<BASE64 ENCODED FILE STRUCTURE>
------WebKitFormBoundary5dcD4Bk7SevSsaMg--
如您所见,Content-Type
和Content-Disposition
有多个标题字段,由boundary
参数分隔。使用NSMutableURLRequest和-setValue:forHTTPHeaderField
时,我只是覆盖以前的值。这是我对标题字段的所有内容:
NSString *boundaryString = @"----WebKitFormBoundary5dcD4Bk7SevSsaMg";
NSString *disposition = @"Content-Disposition: form-data; name=\"ThankYou.mp3\" filename=\"ThankYou.mp3\"";
NSString *type = @"Content-Type: audio/mp3";
NSData *dispositionData = [disposition dataUsingEncoding:NSUTF8StringEncoding];
NSData *typeData = [type dataUsingEncoding:NSUTF8StringEncoding];
NSData *boundaryData = [boundaryString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *songData = [NSMutableData data];
[songData appendData:boundaryData];
[songData appendData:dispositionData];
[songData appendData:typeData];
[songData appendData:mp3Data]; //initiated before. From my mainBundle.
[songData appendData:boundaryData];
[request setHTTPBody:songData];
[self.myAuth authorizeRequest:request];
[request setValue:@"1" forHTTPHeaderField:@"X-Api-Version"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"multipart/form-data; boundary=----WebKitFormBoundary5dcD4Bk7SevSsaMg" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"form-data; name=\"X-Api-Version\"=1.0" forHTTPHeaderField:@"Content-Disposition"];
正如您所看到的,我尝试通过在实际Body Data前面附加边界,Content-Disposition和Content-Type来复制POST请求的结构,然后在末尾附加边界。
在最后,我只在Copy.com上创建了一个名为“ThankYou.mp3”的文件夹,这是当您使用空体数据发出POST请求时会发生的情况。我向你保证身体数据不是空的: - )
有人可以帮助我吗?