Moya: Sending images as multipart form array

Created on 19 Feb 2017  路  1Comment  路  Source: Moya/Moya

Hello,

I wasn't sure this should go here or to SO, but figured I'll give it a shot here, seeing as my last issue was addressed in a great manner.

I'm still working on an app for a friend's website, and now I'm struggling to make Moya send the image as multipart form data to the server. Code is as follows:


enum MyService {
    case create (title: String, abstract: String, file: Data)
}

var multipartBody: [MultipartFormData]? {
    switch self {
    case .create(_, _, let file):
        return [MultipartFormData(provider: .data(file), name: "files", fileName: "image.jpg",
                mimeType: "image/jpeg")]
    default:
        return []
    }
  }

var task: Task {
    return .request
}

Point is, this request needs to send name and abstract as request parameters, and file as part of multipart form data. I can't get my head around how to tackle it. I assume the culprit here is .request as task, as I figured it should be .upload (for now the multipartBody doesn't seem to be executed at all), but I do need to pass request parameters as well. Could you point me in a direction how to handle this issue?

question

Most helpful comment

Ok, solved it. Found the solution after I dug a bit deeper into other issues here (issue #911 being most helpful), moved the MultipartFormData definition to Task. Then I got a fatalError("\(target) is not a multipart upload target.") - a look into Moya source made me double check the request type - and of course it turned out I was trying to POST with a GET request type set for all MyService. Bottom line, it works now.

>All comments

Ok, solved it. Found the solution after I dug a bit deeper into other issues here (issue #911 being most helpful), moved the MultipartFormData definition to Task. Then I got a fatalError("\(target) is not a multipart upload target.") - a look into Moya source made me double check the request type - and of course it turned out I was trying to POST with a GET request type set for all MyService. Bottom line, it works now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tynox picture Tynox  路  3Comments

PlutusCat picture PlutusCat  路  3Comments

fenixsolorzano picture fenixsolorzano  路  3Comments

mwawrusch picture mwawrusch  路  3Comments

pedrovereza picture pedrovereza  路  3Comments