我尝试使用Alamofire 4创建多部分请求。 我不会添加[String:AnyObject]作为一部分。但是当multipartFormData从我的有效负载dict app附加bool或[Int]崩溃时!我的错误在哪里?
let payload: [String: AnyObject] = [
"key1": "val1" as AnyObject,
"key2": true as AnyObject,
"key3" : [1,2,3,4] as AnyObject
]
let requestString = "http://www.url.com/api/action"
Alamofire.upload(multipartFormData: { (multipartFormData) in
for (key, value) in payload {
multipartFormData.append(value.data(using: String.Encoding.utf8.rawValue, allowLossyConversion: false)!, withName: "request", fileName: key, mimeType: "application/json")
}
multipartFormData.append(imageUrl, withName: "fileData", fileName: "image", mimeType: "image/png")
}, to: requestString, method: .post , headers: ["Content-Type": "multipart/form-data"], encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print(progress.fractionCompleted * 100)
})
upload.responseJSON(completionHandler: { (response) in
})
case .failure(let error):
print(error)
}
})
答案 0 :(得分:1)
你发送了错误的参数。请尝试下面的Alamofire 4代码。
let payload: [String: AnyObject] = [
"key1": "val1" as AnyObject,
"key2": true as AnyObject,
"key3" : [1,2,3,4] as AnyObject
]
let requestString = "http://www.url.com/api/action"
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 0.5)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
for (key, value) in payload {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to: requestString, method: .post , headers:nil, encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print(progress.fractionCompleted * 100)
})
upload.responseJSON(completionHandler: { (response) in
})
case .failure(let error):
print(error)
}
})