快速使用Alamofire和PromiseKit将图像上传到Onfido API时状态码422

时间:2018-11-08 09:56:17

标签: ios swift alamofire image-uploading

我正在尝试使用alamofire和PromiseKit将图像上传到Onfido的API,作为响应,我收到状态码422。我上传图像的功能是:

    public func uploadFrontDocument(file: Data,type: String, side: String, appID: String) -> Promise<Void> {
    return Promise { fulfill, reject in
        let request = OnfidoService<documentId>.uploadFrontDocument(file: file, type: type, side: side, appID: appID)
        let URL = try! URLRequest(url: request.baseURL + request.baseApiPath + request.path, method: request.method, headers: request.headers)
        let params: Parameters = request.parameters
        Alamofire.upload(
            multipartFormData: { multipartFormData in
                for (key, value) in params {
                    multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
                }
                multipartFormData.append(file, withName: "frontDL",fileName: "file.jpg", mimeType: "img/jpg")
        }, with: URL) { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload
                    .validate(statusCode: 200 ..< 300)
                    .response { response in
                        print(response)
                        fulfill()
                }
            case .failure(let error):
                reject(error)
            }
        }
    }

并调用此函数,我正在做

    let data = UIImageJPEGRepresentation(croppedImage, 0.8)
                    OnfidoManager.shared.uploadFrontDocument(file: data!,type: "driving_licence", side: "front", appID: appID)
                        .then { docID -> Void in
                            ////////here , je mets dismiss loader
                        }.catch { _ in
                            Logger.error(message: "NoDocID")
                    }

知道我通过邮递员执行请求时,它能正常工作吗,收到状态码422时我做错了什么?

谢谢。

0 个答案:

没有答案