如何使用 alamofire 快速发出 post 请求,参数如下( postman 的屏幕截图)图像是文件类型,标题是文本类型
我正在尝试这样的事情:
let headers = [
"Content-Type": "application/form-data",
"X-App-Token": user.token!
]
Alamofire.upload(multipartFormData:{ multipartFormData in
multipartFormData.append(UIImagePNGRepresentation(imgToSend)!, withName: "image")
multipartFormData.append(titleToSend.data(using: .utf8)!, withName: "title")},
usingThreshold:UInt64.init(),
to: url!,
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)
}
})
但是我得到了错误: [BoringSSL]函数boringssl_session_errorlog:第2868行[boringssl_session_write] SSL_ERROR_SYSCALL(5):库外部操作失败
并且(这很奇怪)调试器进入.success,但是当我记录响应时,api 出现错误
最佳答案
尝试改变
multipartFormData.append(UIImagePNGRepresentation(imgToSend)!, withName: "image")
到
multipartFormData.append(UIImagePNGRepresentation(imgToSend)!, withName: "image", fileName: "sample.png", mimeType: "image/png")
如果您收到如下警告:
line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
你可以简单地忽略它。这仅意味着 TLS 连接上的操作失败,因为 TLS 已通过 close_notify
警报关闭。这种事情本身并不是问题。
You can disable OS logging in Xcode to make them go away. With your project window open, go to Project -> Scheme -> Edit Scheme... and add "OS_ACTIVITY_MODE" to the Environment Variables section and set its value to "disable". When you rerun the app those warnings should now not appear.
关于swift - 如何使用 "form-data"参数发出 alamofire post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48402559/