Hello,
For some reason, I have been asked to remove Parse dependency from my target, but our backend/DB still relies on it.
With Parse-Swift, I was BTW using this code to save a file to our DB:
try ParseFile(
name: url.lastPathComponent,
localURL: url
).save()
On the new version of my app, I am using Alamofire and Moya for HTTP requests purposes. Here is how I defined the file upload endpoint:
import Moya
var apiProvider: MoyaProvider<APIService> {
return MoyaProvider<APIService>()
}
enum APIService {
case fileUpload(URL)
}
extension APIService: TargetType {
var baseURL: URL {
return URL(string: "https://my.url.com/1/")!
}
var headers: [String : String]? {
switch self {
case .fileUpload:
return [
"X-Parse-Application-Id": PARSE_APPLICATION_ID,
"X-Parse-Client-Key": PARSE_CLIENT_KEY,
"X-Parse-Session-Token": USER_SESSION_TOKEN
]
}
var method: Moya.Method {
return .post
}
var path: String {
switch self {
case .fileUpload(let fileURL):
return "files/" + fileURL.lastPathComponent
}
}
var task: Moya.Task {
switch self {
case .fileUpload(let fileURL):
return .uploadFile(fileURL)
}
}
}
And here is how I call this from the app (url
being a local URL fetched from PHPickerViewControllerDelegate
’s didFinishPicking):
public static func upload(_ url: URL) {
apiProvider.request(.fileUpload(url)) { result in
switch result {
case .success(let resp):
break
case .failure(let err):
break
}
}
}
I even compared this request’s call from the ParseFile.save()
one, but I can’t find a reason explaining why I get this error, because both URLRequests
are the same. The error I get:
“code”: 130
“error”: “Invalid file upload.”
Do you have any idea that could explain this?
Thank you for your help