swift - 如何使用 "form-data"参数发出 alamofire post 请求

标签 swift alamofire multipartform-data

如何使用 alamofire 快速发出 post 请求,参数如下( postman 的屏幕截图)图像是文件类型,标题是文本类型 screen shot from 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/

相关文章:

swift - 如何通过单击下一步按钮添加和查看数组中下一张图像的图像

swift - Swift 3 中通过间接调用调用了错误的专用泛型函数

swift - Completion 很快就会被调用

ios - 在 API 请求后更新 UIPageViewController 的正确方法

c# - 将图像和数据作为多部分内容上传 - Windows Phone 8

java - maxUploadsize 和 maxInMemorySize 之间的区别

ios7 - 自定义 UITableViewCell 编辑模式不起作用

ios - 如何在 Swift(iOS) 中按月对 UITableView 数据进行分组?

swift - 永久设置 alamofire cookie

java - Spring Cloud Feign Multipart文件上传