如何使用“表单数据"进行 alamofire 发布请求参数

编程入门 行业动态 更新时间:2024-10-27 06:18:39
本文介绍了如何使用“表单数据"进行 alamofire 发布请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用带有如下参数的 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):操作库外部失败

line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library

你可以简单地忽略它.这只是意味着 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"添加到环境变量"部分并将其值设置为禁用".当您重新运行应用程序时,这些警告现在不应出现.

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.

更多推荐

如何使用“表单数据"进行 alamofire 发布请求参数

本文发布于:2023-10-29 09:33:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   如何使用   参数   数据   alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!