3

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

Am1rFT
  • 227
  • 5
  • 18

2 Answers2

6

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"]
}
arturdev
  • 10,884
  • 2
  • 39
  • 67
  • I did whatever you said and still, I have the problem. – Am1rFT Sep 26 '18 at 07:19
  • @ItanHant What problem exactly do you have? – arturdev Sep 26 '18 at 07:20
  • I get 200 , but in response body if It returns "false" it means there is a problem with the data we send and I check with the admin and she said that It must be a form data request – Am1rFT Sep 26 '18 at 07:22
  • "Form Data" and "Multipart Form Data" are different things. Please check with your admin if it should be just "Form Data" or "Multipart Form Data" – arturdev Sep 26 '18 at 07:23
  • what are the required for form data? – Am1rFT Sep 26 '18 at 07:26
  • @ItanHant Also check the "Content-Type" that you are sending. – arturdev Sep 26 '18 at 07:37
  • I just added a photo, could pls check that – Am1rFT Sep 26 '18 at 07:40
  • @ItanHant Could you also add a screenshot of "Headers" tab? – arturdev Sep 26 '18 at 07:42
  • @ItanHant What "Content-Type" is expecting your server? – arturdev Sep 26 '18 at 07:45
  • I'm not sending ,but postman is sending " "content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", " – Am1rFT Sep 26 '18 at 07:47
  • "content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW is this enough? – Am1rFT Sep 26 '18 at 07:51
  • I can't understand the reason for using multipart form data for this request. Usually, you use multipart when you upload a file to the server, but in your case, you're just doing a simple request. Why just not to ask your admin to switch to "application/json" ? :) – arturdev Sep 26 '18 at 07:51
  • I told him before, but she said it would be needed in future :) – Am1rFT Sep 26 '18 at 07:53
  • Then you definitely should use my first solution. But for debugging the request that you're sending, I suggest you use Charles Proxy Debugging Tool. https://www.charlesproxy.com Install the app, and run your app in a simulator and see what CharlesProxy logged in "Headers" and "Content" sections, and you will find the mistake that you're looking for :) Good luck! – arturdev Sep 26 '18 at 08:00
  • As I'm using moya, it shows all the log that I need! – Am1rFT Sep 26 '18 at 08:55
  • That's true, though CharlesProxy shows much more info. It's up to you – arturdev Sep 26 '18 at 08:56
  • but I didn't understand what anything in Charles as I used, – Am1rFT Sep 26 '18 at 08:58
  • what is FormDataProvider? – Am1rFT Sep 26 '18 at 09:14
3

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

Am1rFT
  • 177
  • 1
  • 2
  • 11