2013-08-21 69 views
1

使用新的Copy.com API。已成功使用OAuth1登錄並請求配置文件數據,並可成功創建文件夾。iOS - 向Copy.com發佈文件上傳請求

如果你看看這裏的API文檔:https://www.copy.com/developer/documentation#api-calls/filesystem並轉到POST對API創建新文件的請求部分,頭文件的說明對我來說有點奇怪。在這裏看到:

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-TypeContent-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"]; 

正如你所看到的,我試圖通過實際身體數據之前附加的邊界,內容處置和Content-Type複製POST請求的結構,然後在最後附加一個邊界。

在此結束時,我只在Copy.com上創建一個名爲「ThankYou.mp3」的文件夾,這是在您使用空正文數據創建POST請求時發生的情況。我向你保證身體數據不是空的:-)

任何人都可以幫我在這裏嗎?

回答