Question

Working with the new Copy.com API. Successfully signed in with OAuth1 and have made requests for profile data, and can successfully create folders.

If you look at the API docs here: https://www.copy.com/developer/documentation#api-calls/filesystem and go to the section on making POST requests to the API to create new files, the instructions for the headers are a little strange to me. See here:

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--

As you can see, there are multiple header fields for Content-Type and Content-Disposition, separated by the boundary parameter. When using NSMutableURLRequest and -setValue:forHTTPHeaderField I just overwrite previous values. Here's what I have for my header fields:

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

As you can see, I tried to replicate the structure of the POST request by appending a boundary, Content-Disposition and Content-Type in front of the actual Body Data, and then append a boundary at the end.

At the end of this I am only created a folder called "ThankYou.mp3" on Copy.com, which is what happens when you make a POST request with empty body data. I assure you the body data is not empty :-)

Can anyone help me out here?

Was it helpful?

Solution

The code used in this class is tested to work: COCopyClient.m

It might be easier to just use the library: copy-mac-ios-sdk

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top