使用Alamofire和BOX API上传多部分表单

时间:2016-10-27 16:27:19

标签: xcode swift3 alamofire box-api

我正在努力让它发挥作用。我正在尝试根据Getting Started section

中的API说明将文件上传到Box

我正在使用Xcode 8,Swift 3和Alamofire 4框架。我用Google搜索并搜索了堆栈溢出。我觉得我很亲密,但文件上传它仍然无法正常工作。

我一直收到来自BOX API的错误说:

  

[结果]:成功:{       code =“bad_request”;       “context_info”= {           错误=(                           {                   message =“'parent'是必需的”;                   name = parent;                   reason =“missing_parameter”;               }           );       };

这是我的代码

func testUpload() {


    let endpoint = "https://upload.box.com/api/2.0/files/content"

    let headers: HTTPHeaders = [
        "Authorization": "Bearer \(tempDeveloperToken)"
    ]

    let jsonAttributes = JSON(["name": "test.pdf", "parent": ["id": "11774646649"]])
    let file = PDFService.getPDF(named: "document")            //Returns file as URL

    let paramAttributes = jsonAttributes.stringValue.data(using: String.Encoding.utf8)

    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(paramAttributes!, withName: "attributes")
            multipartFormData.append(file, withName: "file")
        },
        to: endpoint,
        method: .post,
        headers: headers,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )

}

我尝试了很多不同的方法来添加Params到上传,我缺少什么?

2 个答案:

答案 0 :(得分:1)

经过大量试验后出现错误并花费数小时才能看到这些东西,我终于开始工作了。我没有将参数作为JSON字符串传递,而是决定将其保留为指定的文档,但在编码之前手动转义字符串中的每个双引号。您可以在我的解决方案中看到我创建了一个名为“retest”的新变量,它是替换旧变量“jsonAttributes”的修复程序。我不知道是否有更好的方法来编写这个解决方案,但这对我有用。

func testUpload() {

    let endpoint = "https://upload.box.com/api/2.0/files/content"

    let headers: HTTPHeaders = [
        "Authorization": "Bearer \(tempDeveloperToken)"
    ]

    //let jsonAttributes = JSON(["name": "test.pdf", "parent": ["id": "11774646649"]])
    let retest = "{\"name\":\"test.pdf\", \"parent\":{\"id\":\"11774646649\"}}"
    let paramAttributes = retest.data(using: String.Encoding.utf8) //jsonAttributes.stringValue.data(using: String.Encoding.utf8)

    let file = PDFService.getPDF(named: "document")            //Returns file as URL


    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(paramAttributes!, withName: "attributes")
            multipartFormData.append(file, withName: "file")
        },
        to: endpoint,
        method: .post,
        headers: headers,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )

}

答案 1 :(得分:0)

多部分表单看起来正确。您可以尝试删除parent 标题。它不需要在那里。