python - 使用 Alamofire iOS 以 Base64 将图像上传到服务器

标签 python ios swift iphone base64

我需要在服务器上以 base64 格式发送图像,但我尝试了所有方法,但运气不佳,仍然收到错误 500 Internal Server Error。 但是当我尝试在 POSTMAN 上做同样的事情时,它工作得很好。我附上了 postman 的截图。请帮忙,我几天以来一直坚持这个问题。

func sendImageToServer(image:UIImage){
        SVProgressHUD.show()
        let imageData = image.jpegData(compressionQuality: 0.2)!.base64EncodedData()
        Alamofire.upload(multipartFormData: { (multipartFormdata) in
                multipartFormdata.append(imageData, withName: "image", fileName: "photo.png", mimeType: "image/png")

        }, to: "http://35.180.52.242:5000/predict", method: .post, headers: headers()) { (result) in
            switch result {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (progress) in
                    print("Upload Progress: \(progress.fractionCompleted)")
                })

                upload.responseJSON { response in
                    print(response)
                    SCLAlertView().showError("Error", subTitle: response.response?.description ?? "")
                    SVProgressHUD.dismiss()
                }

            case .failure(let encodingError):
                print(encodingError)
                SVProgressHUD.dismiss()
            }
        }
    }
    func headers()->HTTPHeaders{
        return ["Content-Type": "application/json"] as HTTPHeaders
    }

这就是 Response 的样子。

  • some : { URL: http://35.180.52.242:5000/predict } { Status Code: 500, Headers { "Content-Length" = ( 291 ); "Content-Type" = ( "text/html" ); Date = ( "Fri, 22 Feb 2019 22:26:03 GMT" ); Server = ( "Werkzeug/0.14.1 Python/2.7.15" ); } }

enter image description here

最佳答案

您可以尝试删除手动设置的 header headers: headers() 吗? Alamofire 应该为多部分表单数据设置这些 header ,我猜你正在用 application/json

覆盖它们

关于python - 使用 Alamofire iOS 以 Base64 将图像上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54836069/

相关文章:

swift - AirPrint (UIPrintInteractionController) 使用 iOS 9、swift 2、XCode 7 打印警告

ios - 关闭弹出窗口时执行功能

python - Django框架安装

Python:如何找出列表中是否存在未知元素?

ios-获取唯一标识符,重新安装应用程序后该标识符必须相同

ios - 在 iOS 中切换位置权限

swift - 在 ViewController 中处理菜单项操作

python - 如何在psycopg2中执行非sql命令

python - 有没有办法使用 python 创建 Power BI Visuals?

ios - 将图像添加到 TextField