Search code examples
iosswiftmultipartform-datamultipartmoya

Upload images Multipart With Moya Swift Fully example


how to upload an image using multipart/form-data in Swift With Moya

my code

var multipartBody: [MultipartFormData]? {
    switch self {
    case .registerWith(let registrationObject ):
        if let profileImageData = registrationObject.profileImageData{
        return [MultipartFormData(provider: .data(profileImageData), name: "profile_picture", fileName: "\(registrationObject.phone).jpg",
            mimeType: "image/jpeg")]
        }
        return nil
    default:
        return nil
    }
}

var task: Task {
    switch self {
    case .registerWith:
        if let multipartBody = multipartBody {
            return .upload(.multipart(multipartBody))
        }else{
            return .request
        }
    default:
        return .request
    }
}

but I need to send the imageData in body With key "profileImage"


Solution

  • To upload an image in Multipart using Moya version 10.0.0 and above

    1- Put files you want to upload in MultipartFormData Array

    // put files in MultipartFormData Arry  
       var multipartBody :[Moya.MultipartFormData]?{
        switch self {
        case .registerWith(let imageData , let name ,let description  ):
            // any additional body data or body parms
            let nameDataProvider = MultipartFormData(provider: .data(name.data(using: .utf8)!), name: "name")
            let emailDataProvider = MultipartFormData(provider: .data(email.data(using: .utf8)!), name: "email")
            // image want to upload it or file as a MultipartFormData
            let imageDataProvider = Moya.MultipartFormData(provider: MultipartFormData.FormDataProvider.data(imageData), name: "avatar", fileName: "photo.jpg", mimeType: "image/jpeg")
                return [ imageDataProvider , descriptionDataProvider ]
        default:
            return []
        }
    }
    

    2- make task type of this method is uploadMultipart

    var task: Task {
        switch self {
        case .registerWith:
            // for each method multi part use its multipartBody that declared it in step one needed
            return .uploadMultipart(multipartBody!)
        default:
            guard let parameters = self.parameters else {
                return .requestPlain
            }
            return .requestParameters(parameters: parameters.values, encoding: parameters.encoding)
        }
    }
    

    To upload an image in Multipart using Moya version 9.0.0 and below

    1- Put files you want to upload in MultipartFormData Array

    // put files in MultipartFormData Arry  
    var multipartBody: [MultipartFormData]? {
         switch self {
              case .registerWith(let imageData , let name ,let description  ):
                  return [MultipartFormData(provider: .data(imageData),
                 name: "profile_picture", fileName: "fileName", mimeType: "image/jpeg")]
                  default:
                   return nil
           } 
    }
    

    2- Add a provider of multipartBody to request's body of the method you need it

    var parameters: [String: Any]? {
         switch self {
              case .registerWith(let registrationObject ):
              var body = registrationObject.toJSON()!
              // add the file in body 
              body["profile_picture"] = multipartBody?[0].provider
              return body
         }
    }
    

    3- make task type of this method is upload

    var task: Task {
           switch self {
               case .registerWith:
                  //  multipartBody that in First step
                 return .upload(.multipart(multipartBody))
            }
    }