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

6.8k Views Asked by At

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

2

There are 2 best solutions below

2
On BEST ANSWER

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

18
On

Multipart Form Data cant have other types than Data. So it'd be better to hold a dictionary with type [String:String] and convert it to Data.
Example:

var task: Task {
    case .... {
        let params: [String:String] = ["id":"0", //convert to string
                                       "AddressLine":EntAddressLine.text,
                                       "CityId":"\(selectedCity)",  //convert to string
                                       "Mobile":EntMobile.text,
                                       "PostalCode":EntPostalCode.text,
                                       "ProvinceId":"\(selectedProvince)",//convert to string
                                       "Tel":EntTelephone.text]

        var multipartData = [MultipartFormData]()
        for (key, value) in params {
            let formData = MultipartFormData(provider: .data(value.data(using: .utf8)!), name: key)
            multipartData.append(formData)
        }

        return .uploadMultipart(multipartData)
    }
    ....
}    

===============
EDIT:

For non-multipart requests:

var task: Task {
        case .... {
            let params: [String:Any] = ["id":0,
                                        "AddressLine":EntAddressLine.text,
                                        "CityId":selectedCity,
                                        "Mobile":EntMobile.text,
                                        "PostalCode":EntPostalCode.text,
                                        "ProvinceId":selectedProvince,
                                        "Tel":EntTelephone.text]

            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
        }
        ....
}

var headers: [String : String]? {
    return ["Content-Type":"application/json"]
}