使用alamofire进行响应序列化失败

时间:2018-05-15 09:51:59

标签: ios swift alamofire

我正在尝试使用带有基本身份验证的alamofire将图像上传到服务器。我可以将图像发送到服务器,图像在服务器中,但我无法从服务器获取JSON响应。

这是我用来将缺陷图像发送到服务器的代码

func uploadDefect(defectRemark: String, defectLocation: String, defectImage: UIImage, fileNameImage: String, completion: @escaping(_ errorMessage: String?) -> Void) {

        guard let imgData = defectImage else {return}

        let urlUpload = URLService.uploadDefect.endPoint

        let username = "admin"
        let password = "1234"

        var headers: HTTPHeaders = [:]

        if let authorizationHeader = Request.authorizationHeader(user: username, password: password) {
            headers[authorizationHeader.key] = authorizationHeader.value
        }

        let parameters : [String:Any] = ["defect_remark" : defectRemark, "defect_location": defectLocation, "tenant_id" : tenantID]

        let url = try! URLRequest(url: URL(string: urlUpload)!, method: .post, headers: headers)

        Alamofire.upload(
            multipartFormData: { multipartFormData in
                multipartFormData.append(imgData, withName: "file", fileName: fileNameImage, mimeType: "image/jpeg")

                for (key, value) in parameters {
                    multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
                }
        },
            with: url,
            encodingCompletion: { encodingResult in

                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseJSON { response in

                        print("upload response: \(response)")

                        switch response.result {
                        case .failure(let error) :
                            let message : String
                            if let httpStatusCode = response.response?.statusCode {

                                print("httpStatusCode: \(httpStatusCode)")

                                switch(httpStatusCode) {
                                case 404:
                                    message = "File not found"
                                case 500 :
                                    message = "Internal Error"
                                default:
                                    message = "Connection issue, please make sure you have a good internet access, or please contact IT Support."
                                }
                            } else {
                                message = error.localizedDescription
                            }

                            completion(message)
                        case .success( _) :
                            completion(nil)
                        }
                    }

                case .failure(let encodingError):
                    let messageEncodingError = encodingError.localizedDescription
                    print(encodingError)
                    completion(messageEncodingError)
                    break
                }
        }
        )

我尝试调试代码,并将其打印在我的调试区域

  

上传回复:失败:   responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误   Domain = NSCocoaErrorDomain Code = 3840“字符周围的值无效   1.“UserInfo = {NSDebugDescription =字符1周围的值无效。}))

     

httpStatusCode:201

httpStatusCode是201,但我不明白为什么它的响应失败,据我所知前缀2xx是成功的,我真的不明白为什么响应序列化失败。你能帮我解决这个问题吗?

0 个答案:

没有答案