Search code examples
swiftalamofire

trying to post a multipart whit alamofire 5


I am doing a multipart whit alamofire 5 and i am getting this error:

"Cannot convert value of type 'Result<Any, AFError>' to expected argument type 'OcrResponse'"

I am getting an alert to replace "responseJSON" for "responseDecodable" too, but i get another error that "Generic parameter 'T' could not be inferred" then

Any help to fix that error?

func ocr(image: UIImage, completion: @escaping (Result<OcrResponse, Error>) -> Void) {

    //https://firmas.isquad.es/ocr
    let baseUrl = RESTConstants.ProductionServer.baseURL
    
    AF.upload(multipartFormData: { multiFormPartData in
        
        multiFormPartData.append(UserDefaults.standard.string(forKey: ConstantsHelper.client)!.data(using: .utf8)!, withName: RESTConstants.APIParameterKey.client)
        multiFormPartData.append(UserDefaults.standard.string(forKey: ConstantsHelper.token)!.data(using: .utf8)!, withName: RESTConstants.APIParameterKey.token)
        multiFormPartData.append(image.jpegData(compressionQuality: 1.0)!, withName: RESTConstants.APIParameterKey.image, fileName: "imagenFactura.jpeg", mimeType: "image/jpg")
        
    }, to: "\(baseUrl)/ocr", usingThreshold: UInt64(), method: .post, headers: nil).responseString(completionHandler: { (response) in
        
        NSLog("URL: \(response.request?.url?.absoluteURL.absoluteString ?? "nil")")
        switch response.result {

        case let .success(value):
            NSLog("RESPONSE: \(value)")
        case let .failure(error):
            NSLog("ERROR: \(error.localizedDescription)")
        }
    }).responseJSON { (response) in
        
        switch response.result {
            
        case .success:
            completion(Result.success(response.result)) // <- Here is the error
        case .failure:
            break
        }
    }
}

Postman: postman


Solution

  • Fixed it:

    func ocr(image: UIImage, completion: @escaping (Result<OcrResponse, Error>) -> Void) {
    
        let baseUrl = RESTConstants.ProductionServer.baseURL
        
        AF.upload(multipartFormData: { multiFormPartData in
            
            multiFormPartData.append(UserDefaults.standard.string(forKey: ConstantsHelper.client)!.data(using: .utf8)!, withName: RESTConstants.APIParameterKey.client)
            multiFormPartData.append(UserDefaults.standard.string(forKey: ConstantsHelper.token)!.data(using: .utf8)!, withName: RESTConstants.APIParameterKey.token)
            multiFormPartData.append(image.jpegData(compressionQuality: 1.0)!, withName: RESTConstants.APIParameterKey.image, fileName: "imagenFactura.jpeg", mimeType: "image/jpg")
            
        }, to: "\(baseUrl)/ocr", usingThreshold: UInt64(), method: .post, headers: nil).responseString(completionHandler: { (response) in
            
            NSLog("URL: \(response.request?.url?.absoluteURL.absoluteString ?? "nil")")
            switch response.result {
    
            case let .success(value):
                NSLog("RESPONSE: \(value)")
            case let .failure(error):
                NSLog("ERROR: \(error.localizedDescription)")
            }
        }).responseDecodable(of: OcrResponse.self) { (response) in
            
            switch response.result {
                case .success(let response):
                completion(Result.success(response))
                
            case .failure(let error):
                #if DEBUG
                    NSLog("Error \(error.localizedDescription)")
                #endif
                completion(Result.failure(error))
            }
        }
    }