6

The request I am getting on server side is missing the content-length of each multipart.

I tried to change the headers but it still won't display.

I am successfully sending image file and some data from device/simulator to server using Alamofire/ multipart form-data.

 var parameters = […]

    let url = try! URLRequest(url: “URL”, method: .post, headers: ["Content-type": "multipart/form-data"])

    Alamofire.upload(multipartFormData: { (multipartFormData) in
        for (key, value) in parameters {
            multipartFormData.append((value.data(using: String.Encoding.utf8)!), withName:key)

        }
        multipartFormData.append(UIImageJPEGRepresentation(image!, 0.5)!, withName: “Image”, mimeType: "image/jpeg")

    }, to: "\(url)" , encodingCompletion: {(encodingResult) in

        switch encodingResult {
        case .success(let upload, _, _):
            //print(result)
            upload.uploadProgress(closure: { (Progress) in

                self.progressView.progress = Float(Progress.fractionCompleted)
                print("Upload Progress: \(Progress.fractionCompleted)")
            })


            print("REQUEST = \(request)")
            print(encodingResult)

            upload.responseJSON { response in

                //self.delegate?.showSuccessAlert()
                print( response.request)  // original URL request
                print(response.response) // URL response
                print(response.data)     // server data
                print(response.result)   // result of response serialisation
                // self.showSuccesAlert()
                //self.removeImage("frame", fileExtension: "txt")
                if let JSON = response.result.value {
                    print("JSON: \(JSON)")
                }
            }

        case .failure(let encodingError):
            //self.delegate?.showFailAlert()
            print(encodingError)
        }
    })

Update: Even after keeping the headers: nil , it won't show up.

Received from alamofire:-

--alamofire.boundary.cc9684e085522290
Content-Disposition: form-data; xyz="abc"

123456
--alamofire.boundary.cc9684e085522290

How to get the content-length of each multipart data passed?

1 Answers1

3

Try changing headers to

headers: ["Content-Length"]

amish
  • 335
  • 4
  • 17