[SWIFT] POST image with multipart / form-data using Moya

I'm a little addicted to it now, so a memo

var task: Task {
        switch self {
              
        case .hoge(let files):
        //files is[Data]

            let fileMFDataArray: [MultipartFormData] = files.enumerated().map({ (index, data) in
                MultipartFormData(provider: .data(data),
                                  name: "file", //This is the key
                                  fileName: "\(Int(Date().timeIntervalSince1970))\(index)", //UnixTime +Generate file name of index number
                                  mimeType: "multipart/form-data")
            })
            return .uploadMultipart(fileMFDataArray)
    
        }
    }

Don't forget to set the header to ["Content-Type": "multipart / form-data"] and you're done!

Recommended Posts

POST image with multipart / form-data using Moya
Post an image with POSTMAN
Receive multipart / form-data with Jerjey (Jax-rs)
Proxy server with squid using docker image
Post an image with POSTMAN
First post
First post
First post
Using Pair with OpenJDK
Register your own Docker image with ECR using AWS CLI