Alamofire: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

前端 未结 2 1258
感动是毒
感动是毒 2020-12-12 08:20

I am trying to upload data through alamofire including images. But it returns

responseSerializationFailed(Alamofire.AFError.ResponseSerializationFai

相关标签:
2条回答
  • 2020-12-12 08:58

    Try this for uploading image with parameter with alamofire

     Alamofire.upload(multipartFormData: { (multipartFormData) in
    
            let imageData: NSData? = UIImageJPEGRepresentation(image, 0.5) as NSData?
            if imageData != nil {
    
                multipartFormData.append(imageData! as Data, withName: strImageKey, fileName: "imagep.jpeg", mimeType: "imagep/jpeg")
    
                for (key, value) in dictParams {
                    let data = "\(value)".data(using: .utf8)
                    multipartFormData.append(data! as Data, withName: key)
                }
            }
    
        }, with: requestName,encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
    
                upload.responseJSON { response in
                    log.debug("Image(s) Uploaded successfully:\(response)")
                    //Networking.handleApiResponse(response)
                }
            case .failure(let encodingError):
                log.debug("encodingError:\(encodingError)")
                // Networking.handleApiResponse(response)
    
                Util.showAlertWithMessage(msgSorry, title:"Error")
            }
            completionHandler!(encodingResult)
        }
        )
    
    0 讨论(0)
  • 2020-12-12 09:05

    Just change from responseJSON to responseString

    Alamofire.upload(multipartFormData: { (multipartFormData) in
    
                multipartFormData.append((userid?.data(using: String.Encoding.utf8))!, withName: "user_id")
    
                multipartFormData.append((self.addUserBtnTag?.description.data(using: String.Encoding.utf8))!, withName: "slot")
    }, to: urlString, method: .post, encodingCompletion: { (result) in
     switch result {
                case .success(let upload, _, _):
    
                    upload.responseString { response in
    
                        print(response.result)
                        print("Response : ", response)
    
                        if response.result.isSuccess
                        {
                            let JSON = response.result.value as? NSDictionary
    
                            print("EditJSON : ", JSON!)
                        }
                    }
                case .failure(let encodingError):
                    print(encodingError)
                }
    
            })
    
    0 讨论(0)
提交回复
热议问题