ALAMOFIRE: responseSerializationFailed(Аламофир.Аферрор.ResponseSerializationFailureReason.inputDataNilOrZeroLength)


Я пытаюсь загрузить данные через alamofire, включая изображения.Но он возвращается

ResponseSerializationFailed(Alamofire.Аферрор.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

-- и мой код таков:

    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.responseJSON { 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)
            }

        })
Но на самом деле я передаю более десяти параметров через alamofire.Заранее спасибо
2 2

2 ответа:

Просто измените с responseJSON на 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)
            }

        })

Попробуйте это для загрузки изображения с параметром с 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)
    }
    )