问题描述
如何使用带有如下参数的 alamofire 快速发出 post 请求(来自邮递员的截图)图像是文件类型,标题是文本类型
How to make post request in swift using alamofire with parameters as below(screenshot from postman)image is file-type, title is text-type
我正在尝试这样:
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 出现错误
and (this is weird) debugger goes into .success but when I log response there is error from api
推荐答案
尝试更改
multipartFormData.append(UIImagePNGRepresentation(imgToSend) !, withName: "image")
到
multipartFormData.append(UIImagePNGRepresentation(imgToSend) !, withName: "image", fileName: "sample.png", mimeType: "image/png")
如果您收到如下警告:
第 2878 行 [boringssl_session_write] SSL_ERROR_SYSCALL(5):操作库外部失败
你可以简单地忽略它.这只是意味着 TLS 连接上的操作失败,因为 TLS 已通过 close_notify
警报关闭.这种事情本身不是问题.
You can simply ignore it. This simply means that an operation on the TLS connection failed because the TLS was closed via the close_notify
alert. This sort of thing is not a problem in and of itself.
您可以在 Xcode 中禁用操作系统日志记录以使其消失.打开项目窗口后,转到 Project -> Scheme -> Edit Scheme... 并将OS_ACTIVITY_MODE"添加到环境变量"部分并将其值设置为禁用".当您重新运行应用程序时,这些警告现在不应出现.
这篇关于如何使用“表单数据"进行 alamofire 发布请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!