0

I am trying to upload an image to the server using alamofire with basic authentication. I can send the image to the server, the image is in the server, but I can't get the JSON response from the server.

here is the code I use to send a defect image to the server

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
                }
        }
        )

I try to debug the code, and it printed in my debugging area

upload response: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 1." UserInfo={NSDebugDescription=Invalid value around character 1.}))

httpStatusCode: 201

the httpStatusCode is 201, but I don't understand why it is failed response, as far as I know prefix 2xx is success, I really don't understand why response Serialization Failed. could you please help me to solve this problem?

sarah
  • 3,819
  • 4
  • 38
  • 80
  • Refer this link : https://stackoverflow.com/questions/36997007/swift-error-domain-nscocoaerrordomain-code-3840-invalid-value-around-character?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Sharad Chauhan May 15 '18 at 09:55
  • Possible duplicate of [Alamofire custom parameters](https://stackoverflow.com/questions/39560694/alamofire-custom-parameters) – Tamás Sengel May 15 '18 at 13:57

0 Answers0