Upload file to Box.com via RESTful API using NSURLSession / Swift 2.0
I would like to upload a log file from our iOS app directly to a folder on Box.com. Reading the api for Box this functionality is provided with REST webservices. The examples in the api are cURL which work out of the box (sorry, no pun intended) from the terminal.
Our app is Swift based, and I am using NSURLSession to make the request. After too many attempts to count, and close examination of the limited blogs on this subject (iOS / Box) I still have not managed to upload a test file.
One frustrating thing is that the api appears to be wrong, as seen here, but following the format suggested in this answer did not help.
Here is the NSURLSession request code from my latest attempt:
let path = NSBundle.mainBundle().pathForResource("testFile", ofType: "txt") let data: NSData? = NSData(contentsOfFile: path!) let url = NSURL(string: "https://upload.box.com/api/2.0/files/content")! let request = NSMutableURLRequest(URL: url) let boundary = generateBoundaryString()request.HTTPMethod = "POST"request.addValue("Bearer q1ckXDSTC0EcsCJiLtW638o5x5roWabb", forHTTPHeaderField: "Authorization") let body = NSMutableData()body.appendData("Content-Type: multipart/form-data; boundary=\(boundary)\n".dataUsingEncoding(NSUTF8StringEncoding)!)body.appendData("--\(boundary)\n".dataUsingEncoding(NSUTF8StringEncoding)!)body.appendData("Content-Disposition: form-data; name=\"parent_id\"\n".dataUsingEncoding(NSUTF8StringEncoding)!)body.appendData("Content-Type: text/plain\n".dataUsingEncoding(NSUTF8StringEncoding)!)body.appendData("0\n".dataUsingEncoding(NSUTF8StringEncoding)!)body.appendData("--\(boundary)\n".dataUsingEncoding(NSUTF8StringEncoding)!)body.appendData("Content-Disposition: form-data; name=\"filename\"; filename=\"testFile.txt\"\n".dataUsingEncoding(NSUTF8StringEncoding)!)body.appendData("Content-Type: text/plain\n".dataUsingEncoding(NSUTF8StringEncoding)!)body.appendData(data!)body.appendData("\n--\(boundary)--".dataUsingEncoding(NSUTF8StringEncoding)!)request.HTTPBody = body let task = NSURLSession.sharedSession().uploadTaskWithRequest(request, fromData: data!, completionHandler: {(data,response,error) in if(error != nil) { NSLog("Error message = \(error.debugDescription)") } if(response != nil) { NSLog("Response = \(response)") } if(data != nil) { NSLog("Data = \(data!)") } });task.resume()
The server responds with a 405 error.
Response = Optional(<NSHTTPURLResponse: 0x157de26d0> { URL: https://upload.box.com/api/2.0/files/content } { status code: 405, headers { Age = 0; Allow = "GET, OPTIONS, HEAD"; Connection = "keep-alive"; "Content-Length" = 0; "Content-Type" = "text/html; charset=UTF-8"; Date = "Thu, 13 Oct 20**removed**:49:49 GMT"; Server = ATS; } })
From the example above the request header is:
Request headers - Optional(["Authorization": "Bearer q1ckXDSTC0EcsCJiLtW638o5x5roWabb"])!
And the request body is:
body - Content-Type: multipart/form-data; boundary=0B1D5883-9EA6-4D6B-9FEC-C51D817D5975--0B1D5883-9EA6-4D6B-9FEC-C51D817D5975Content-Disposition: form-data; name="parent_id" Content-Type: text/plain0 --0B1D5883-9EA6-4D6B-9FEC-C51D817D5975Content-Disposition: form-data; name="filename"; filename="testFile.txt" Content-Type: text/plainHello --0B1D5883-9EA6-4D6B-9FEC-C51D817D5975--
We have tried to disseminate the cURL request with Wireshark but as it is an SSL request this didn't help much.
The verbose version of the cURL request looks like this:
0000: POST /api/2.0/files/content HTTP/1.1 0026: Host: upload.box.com003c: User-Agent: curl/7.47.0 0055: Accept: */* 0062: Authorization: Bearer o6ojjxpfECdKrBzIUhiRyRKHTTXg8Sll 009a: Content-Length: 368 00af: Expect: 100-continue 00c5: Content-Type: multipart/form-data; boundary=-------------------- 0105: ----ed667cff7b6570a1 011b: == Info: Done waiting for 100-continue => Send data, 285 bytes (0x11d) 0000: --------------------------ed667cff7b6570a1 002c: Content-Disposition: form-data; name="attributes" 005f: 0061: {"name":"testFile.txt", "parent":{"id":"0"}} 008f: --------------------------ed667cff7b6570a1 00bb: Content-Disposition: form-data; name="file"; filename="testFile. 00fb: txt" 0101: Content-Type: text/plain 011b: => Send data, 35 bytes (0x23) 0000: This is a test file. => Send data, 48 bytes (0x30) 0000: 0002: --------------------------ed667cff7b6570a1--
Which we have also tried to replicate exactly, but the problem remains.
If anyone has achieved this successfully I would really appreciate some direction or suggestions
サインインしてコメントを残してください。
コメント
0件のコメント