Moya 10.0.1を使用していますが、サーバーに画像をアップロードしようとすると問題が発生します。
私は Multipart Upload example に従いました、そしてこれは私のセットアップコードです:
typealias UpdateUserAvatarParameters = (userId: Int, image: UIImage)
enum APITarget {
case updateUserAvatar(parameters: UpdateUserAvatarParameters)
}
extension APITarget: TargetType {
public var baseURL: URL { return URL(string: "http://domain/api")! }
public var path: String {
switch self {
case .updateUserAvatar: return "/postuserimage"
}
}
public var method: Moya.Method {
switch self {
default: return .post
}
}
public var task: Task {
switch self {
case .updateUserAvatar(let parameters):
let imageData = UIImagePNGRepresentation(parameters.image) ?? Data()
let userIdData = parameters.userId.string.data(using: String.Encoding.utf8) ?? Data()
let imageMultipartFormData = MultipartFormData(provider: .data(imageData), name: "img", fileName: "user_avatar.jpeg", mimeType: "image/jpeg")
let userIdMultipartFormData = MultipartFormData(provider: .data(userIdData), name: "cusId")
return .uploadMultipart([imageMultipartFormData, userIdMultipartFormData])
}
}
public var sampleData: Data {
return Data()
}
public var headers: [String : String]? {
switch self {
case .updateUserAvatar: return ["Content-type" : "multipart/form-data"]
default: return ["Content-type" : "application/json"]
}
}
}
しかし、リクエストすると、MoyaErrorが発生しました。
let parameters = UpdateUserAvatarParameters(userId: 1, image: pickedImage)
provider.request(.updateUserAvatar(parameters: parameters), completion: { result in
switch result {
case .success(let response):
do {
try _ = response.filterSuccessfulStatusCodes()
print("200 - 299: \(response.data)")
} catch {
print(error) // This code will run because the statusCode is 500
}
case .failure(let error):
print("Failure: \(error)")
}
})
私は何が間違っていたのかわかりません。また、statusCode 500とは何なのかわかりません。なぜ誰か知っていますか?
さらに、同じ設定(url、parameters ...)でAlamofireを使用してリクエストを行うと、すべてが正常に機能します。
Alamofire.upload(multipartFormData: { multipartFormData in
let imageData = UIImagePNGRepresentation(pickedImage) ?? Data()
let userIdData = userId.string.data(using: String.Encoding.utf8) ?? Data()
multipartFormData.append(imageData, withName: "img", fileName: "user_avatar.jpeg", mimeType: "image/jpeg")
multipartFormData.append(userIdData, withName: "cusId")
}, to: "http://domain/api/postuserimage", encodingCompletion: { result in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { progress in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON(completionHandler: { response in
print(response.result.value)
})
case .failure(let encodingError):
print(encodingError)
}
})
var task: Task {
switch self {
case .updateProfilePic(let memberID, let image):
let imageData = UIImageJPEGRepresentation(image, 1.0)
let memberIdData = memberID.data(using: String.Encoding.utf8) ?? Data()
var formData: [Moya.MultipartFormData] = [Moya.MultipartFormData(provider: .data(imageData!), name: "user_img", fileName: "user.jpeg", mimeType: "image/jpeg")]
formData.append(Moya.MultipartFormData(provider: .data(memberIdData), name: "member_id"))
return .uploadMultipart(formData)
}
Moyaマルチパートリクエストでは、パラメーターをキー名と共にマルチパートデータフォームとして渡す必要があります。
moya Swiftでのマルチパートリクエストを使用した画像のアップロードの詳細。 setp 1
import Foundation
import Moya
enum ImageService {
case updateUserAvatar(user_id: Int, cover_image: UIImage)
}
extension ImageService: TargetType {
public var baseURL: URL { return URL(string: "https://api.shoclef.com/api")! }
public var path: String {
switch self {
case .updateUserAvatar(_,_): return "/updatecoverimage"
}
}
public var method: Moya.Method {
switch self {
case .updateUserAvatar(_, _):
return .post
}
}
var task: Task {
switch self {
case .updateUserAvatar(let user_id, let cover_image):
let imageData = UIImageJPEGRepresentation(cover_image, 1.0)
let memberIdData = "\(user_id)".data(using: String.Encoding.utf8) ?? Data()
var formData: [Moya.MultipartFormData] = [Moya.MultipartFormData(provider: .data(imageData!), name: "cover_image", fileName: "asdas.png", mimeType: "image/jpeg")]
formData.append(Moya.MultipartFormData(provider: .data(memberIdData), name: "user_id"))
return .uploadMultipart(formData)
}
}
public var sampleData: Data {
return Data()
}
public var headers: [String : String]? {
switch self {
case .updateUserAvatar: return ["Content-type" : "application/json"]
}}}
ステップ2
@IBOutlet weak var myImage: UIImageView!
var provider = MoyaProvider<ImageService>()
@IBAction func uploadImage(_ sender: UIButton) {
provider.request(ImageService.updateUserAvatar(user_id: 1163, cover_image: myImage.image!)) { result in
switch result {
case .success(let response):
do {
let image = try JSONDecoder().decode(Updatecoverimage.self, from: response.data)
print(image.data)
} catch {
print(error) // This code will run because the statusCode is 500
}
case .failure(let error):
print("Failure: \(error)")
}
}
}
}
struct Updatecoverimage: Codable {
let status, data: String
}