ios – Required request half ‘file’ is just not current In Swift

0
17


I’m making an attempt to add profile picture to server utilizing multipart file add.
My code is

func uploadPhoto(picture: UIImage, filePath: String, completion: @escaping uploadPhotoClosure) {
    
    let imageData = picture.jpegData(compressionQuality: 0.3)
    
    let url = URL(string: "https://mhms1-dev.hyundai.co.in/service/widespread/uploadFile")!
    let request = NSMutableURLRequest(url: url)
    
    request.httpMethod = "POST"
    let boundary = "---------------------------14737809831466499882746641449"
    let contentType = "multipart/form-data; boundary=(boundary)"
    request.addValue(contentType, forHTTPHeaderField: "Content material-Sort")
    request.addValue("utility/json", forHTTPHeaderField: "Settle for")
    
    //        print(request.allHTTPHeaderFields)
    
    let physique = NSMutableData()
    
    physique.append("rn--(boundary)rn".information(utilizing: String.Encoding.utf8)!)
    physique.append("Content material-Disposition: form-data; identify="field_name"rnrn".information(utilizing: String.Encoding.utf8)!)
    physique.append("field_mobileinfo_image".information(utilizing: String.Encoding.utf8, allowLossyConversion: true)!)
    
    physique.append("rn--(boundary)rn".information(utilizing: String.Encoding.utf8)!)
    physique.append("Content material-Disposition: form-data; identify="information[field_mobileinfo_image]"; filename="img.jpg"rn".information(utilizing: String.Encoding.utf8)!)
    physique.append("Content material-Sort: utility/octet-streamrnrn".information(utilizing: String.Encoding.utf8)!)
    physique.append(imageData!)
    physique.append("rn--(boundary)rn".information(utilizing: String.Encoding.utf8)!)
    
    //  let request = self.serviceManager.request(sort: .submit, path: "/cu/buyer/updateCustomerAddress", headers: nil, physique: nil)
    
    self.serviceManager.async(request: request as URLRequest) { (innerClosure) in
        do {
            let response = strive innerClosure()
            guard let json = response.jsonObject else {
                completion({ throw JSONErrorType.parsingError })
                return
            }
            let jsonData = strive JSONSerialization.information(withJSONObject: json, choices: [])
            let responseModel = strive JSONDecoder().decode(EditProfilePhotoUploadResponse.self, from: jsonData)
            completion({ return responseModel })
        } catch {
            completion({ throw error })
        }
    }
}
//servicemanager class
func async(request: URLRequest, completion: @escaping ServiceManagerAsyncCompletion) {
    ServiceManager.log(request)
    
    let job = self.urlSession.dataTask(with: request) { (information, response, error) in
        guard error == nil else {
            DispatchQueue.major.async {
                completion({ throw error! })
            }
            return
        }
        
        guard let response = response as? HTTPURLResponse else {
            DispatchQueue.major.async {
                completion({ throw ServiceErrorType.invalidResponse })
            }
            return
        }
        
        ServiceManager.log(response, withBody: information)
        //right here dealing with standing code
        
    }
}
}

And the error from server is under

{
“standing” : 500,
“message” : “Required request half ‘file’ is just not current”,
“timestamp” : “2022-08-05T05:30:55.415+0000”,
“path” : “/uploadFile”,
“error” : “Inside Server Error”
}

Any solutions?

LEAVE A REPLY

Please enter your comment!
Please enter your name here