使用 Alamofire 和 BOX 的多部分表单上传 API
Multi-part form upload using Alamofire and BOX API
我很难让它发挥作用。我正在尝试按照 Getting Started section
中的 API 说明将文件上传到 Box
我正在使用 Xcode 8、Swift 3 和 Alamofire 4 框架。我用谷歌搜索并搜索了堆栈溢出。我感觉我已经接近了,但是文件上传还是不行。
我不断收到来自 BOX API 的错误消息:
[Result]: SUCCESS: {
code = "bad_request";
"context_info" = {
errors = (
{
message = "'parent' is required";
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)
}
}
)
}
我尝试了很多不同的方法来为上传添加参数,我错过了什么?
多部分表单看起来正确。您可以尝试删除 parent
header。它不需要在那里。
经过多次试错并花了几个小时研究这个东西,看看有什么问题,我终于让它工作了。我没有将参数作为 JSON 字符串传递,而是决定将其保留为指定的文档,但在编码之前手动转义字符串中的每个双引号。您可以在我的解决方案中看到我创建了一个名为 "retest" 的新变量,它是替换旧变量 "jsonAttributes"[=15= 的修复方法].我不知道是否有更好的方法来编写此解决方案,但这对我有用。
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)
}
}
)
}
我很难让它发挥作用。我正在尝试按照 Getting Started section
中的 API 说明将文件上传到 Box我正在使用 Xcode 8、Swift 3 和 Alamofire 4 框架。我用谷歌搜索并搜索了堆栈溢出。我感觉我已经接近了,但是文件上传还是不行。
我不断收到来自 BOX API 的错误消息:
[Result]: SUCCESS: { code = "bad_request"; "context_info" = { errors = ( { message = "'parent' is required"; 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)
}
}
)
}
我尝试了很多不同的方法来为上传添加参数,我错过了什么?
多部分表单看起来正确。您可以尝试删除 parent
header。它不需要在那里。
经过多次试错并花了几个小时研究这个东西,看看有什么问题,我终于让它工作了。我没有将参数作为 JSON 字符串传递,而是决定将其保留为指定的文档,但在编码之前手动转义字符串中的每个双引号。您可以在我的解决方案中看到我创建了一个名为 "retest" 的新变量,它是替换旧变量 "jsonAttributes"[=15= 的修复方法].我不知道是否有更好的方法来编写此解决方案,但这对我有用。
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)
}
}
)
}