0

I am developing an application in Swift language. I set up a backend service with amazon s3 service and fast api. I want to save an image with .png extension to this backend service. I am getting 422 "422 Unprocessable Entity" error from the server while sending the photo. I know this error occurs because the format is wrong but I couldn't fix the problem. I am sharing the part of the code I wrote in swift and the fast-api screenshot.

format inputs

`func uploadImage2(paramName: String, fileName: String, image: UIImage, uid: String) {
        let urlString = ""
        let url = NSURL(string: urlString)!
        let requestURL = NSMutableURLRequest(url: url as URL)
        requestURL.httpMethod = "POST"
        requestURL.addValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
        
        let body = NSMutableData()
        body.append("file=@\(image)".data(using: String.Encoding.utf8)!)
        body.append("type=image/png".data(using: String.Encoding.utf8)!)
        requestURL.httpBody = body as Data
        
        let task = URLSession.shared.dataTask(with: requestURL as URLRequest){ data,response,error in
            if let data = data {
                let sendedData = String(data: data, encoding: .utf8)
                print("Succesful + \(String(describing: sendedData))")
            } else {
                print("Error: \(String(describing: error))")
            }
        }
        task.resume()
    }`

I'm having trouble uploading photos to the server.

  • 1
    I think your implementation for `multipart/form-data` is wrong. E.g. you don´t have a boundary. Which is mandatory as far as I know. – burnsi Dec 05 '22 at 23:54
  • Seem to relate with this question: https://stackoverflow.com/questions/29623187/upload-image-with-multipart-form-data-ios-in-swift – goat_herd Dec 06 '22 at 02:39

0 Answers0