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?
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.
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
MultipartFormDatadefinition toTask. Then I got afatalError("\(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 toPOSTwith aGETrequest type set for allMyService. Bottom line, it works now.