Search code examples
iosswiftxcodemultipartform-datamoya

multipart form-data in moya with an int value in swift


I'm trying to send a form data with post request to API with moya. I searched the stack overflow and found some questions which they were similar to mine but I'm getting some error which I'll post it below. This and This are the two references that I read for my problem [![These are the keys that I'm supposed to send][3]][3]

Cannot convert value of type 'Int' to expected argument type 'Data'

even I tried the code below:

 let memberIdData = memberID.data(using: String.Encoding.utf8) ?? Data()




let data = ["id":0,
                    "AddressLine":EntAddressLine.text,
                    "CityId":selectedCity,
                    "Mobile":EntMobile.text,
                    "PostalCode":EntPostalCode.text,
                    "ProvinceId":selectedProvince,
                    "Tel":EntTelephone.text] as [String : Any]

how exactly should I fix this

UPDATE:

    case .AddAddress(let data):
            var multipartData = [MultipartFormData]()
            for (key, value) in params! {
                let Provience = MultipartFormData.init(provider: .data("\(data)".data(using: String.Encoding.utf8)!), name: "id")

                multipartData.append(Provience)
            }
            return .uploadMultipart(multipartData)

enter image description here

enter image description here


Solution

  • nothing special you should do!!!

    public var task: Task {
        switch self {
    
        case .AddAddress(let data):
            return .requestParameters(parameters: data, encoding: URLEncoding.default)
    
        }
    
        }
    

    try this , this should help