Moya: fatal error: Test is not a multipart upload target

Created on 9 Jan 2017  路  2Comments  路  Source: Moya/Moya

Hi, I'm using Moya (8.0.0-beta.6) for uploading image and post parameters.

But I'm getting fatal error: Test("username") is not a multipart upload target error. My request's multipart body is empty.

I'm using following code:

This is APIProvider class

    class APIProvider {

    static let sharedInstance = APIProvider()

    internal static func endpointClosure(target: Appname) -> Endpoint<Appname> {
        let sampleResponseClosure = { return EndpointSampleResponse.networkResponse(200, target.sampleData) }
        let method = target.method
        let path = target.path
        let parameters = target.parameters
        return Endpoint<Appname>(url: Constants.API_URL + path, sampleResponseClosure: sampleResponseClosure, method: method, parameters: parameters, parameterEncoding: URLEncoding() )
    }

    var provider = RxMoyaProvider<Appname>(endpointClosure: APIProvider.endpointClosure)

    private init() {
        provider = RxMoyaProvider<Appname>(endpointClosure: APIProvider.endpointClosure)
    }

    //     MARK: - Upload image

    func getTestImage(name : String, image : UIImage) -> Observable<Test> {
        return self.provider
            .request(Appname.Test(name: name, image: image))
            .debug()
            .mapObject(Test.self)
    }

This is Endpoint:

      enum Appname {

    case Test(name : String, image : UIImage)
    }

      extension Nutrick : TargetType {
    var baseURL: URL {
        let url =  URL(string: Constants.API_URL)!
        print(url)
        return url
    }

    var path: String {
        switch self {
        case .Test:
            return "image"
        }

    }

    var method: Moya.Method {
        switch self {
        case .Test:
            return .post
        }
    }

    var parameters: [String: Any]? {
        switch self {
        case .Test(let name, _):
            return ["name" : name]
        }
    }

    var multipartBody: [MultipartFormData]? {
        switch self {
        case .Test(_, let image):

            let imageData = UIImageJPEGRepresentation(image, 1.0)
            let formData: [MultipartFormData] = [MultipartFormData(provider: .data(imageData!), name: "image", fileName: "photo.jpg", mimeType: "image/jpeg")]
            return formData


        default:
            return []
        }
    }

    var sampleData: Data {

        switch self {
        case .Test:
             return "{\"name\":\"1\",\"path\":\"User logged in successfully.\"}".data(using: String.Encoding.utf8)!
        }

    }

    var task : Task {
        switch self {

        case .Test:
            return .upload(UploadType.multipart([]))
        default:
            return .request
        }
    }
    }

If I set breakpoints on parameters and multipartBody vars then parameters breakpoint getting called but multipartBody breakpoint is not getting called. Is anything missing in APIProvider initialization? I already set task type to upload.

Please help me to fix this.

question

Most helpful comment

Hi,

The issue is fixed.

I'm sending an image in the multipart body instead of task.

Solution is:

 var task : Task {
        switch self {
        case .Test(_, let image):

            let imageData = UIImageJPEGRepresentation(image, 1.0)
            let formData: [MultipartFormData] = [MultipartFormData(provider: .data(imageData!), name: "image", fileName: "photo.jpg", mimeType: "image/jpeg")]
            return .upload(UploadType.multipart(formData))
        default:
            return .request
        }
    }

Thanks

All 2 comments

Hi,

me to have same issue, it was working before in previous version. Pleas suggest where i am wrong in implementation??

Hi,

The issue is fixed.

I'm sending an image in the multipart body instead of task.

Solution is:

 var task : Task {
        switch self {
        case .Test(_, let image):

            let imageData = UIImageJPEGRepresentation(image, 1.0)
            let formData: [MultipartFormData] = [MultipartFormData(provider: .data(imageData!), name: "image", fileName: "photo.jpg", mimeType: "image/jpeg")]
            return .upload(UploadType.multipart(formData))
        default:
            return .request
        }
    }

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tynox picture Tynox  路  3Comments

pedrovereza picture pedrovereza  路  3Comments

fenixsolorzano picture fenixsolorzano  路  3Comments

PlutusCat picture PlutusCat  路  3Comments

syq7970 picture syq7970  路  3Comments