Moya / Moya

Network abstraction layer written in Swift.
https://moya.github.io
MIT License
15.15k stars 1.99k forks source link

How do you send a nil array in MultipartFormData? #1721

Closed sedwo closed 6 years ago

sedwo commented 6 years ago

I'm trying to send the following dictionary within a multipart form:

let parametersDict: [String: Any] = [
    "object_id": sign.id,
    "client_last_updated": sign.clientLastUpdated!,
    "number": sign.number,
    "state": sign.workflowStateID!,
    "location": sign.locationID!,
    "x": sign.positionX,
    "y": sign.positionY,
    "tag_list": Array(sign.tagsList)
    ]

The "tag_list" array is my issue. As it can be null. And even when I convert it to an empty array in JSON (eg.[]), it still inserts that as a string.

var multipartBody: [Moya.MultipartFormData]? {
    switch self {
    case .update(_, _, let parametersDict, _):
        var formData: [Moya.MultipartFormData] = []

        if let parameters = parametersDict {
            for (key, value) in parameters {

                if value is [String] {
                    let parameterArray = value as! [String]
                    if parameterArray.count > 0 {
                        let jsonArr = try? JSONEncoder().encode(value as! [String])
                        formData.append(Moya.MultipartFormData(provider: .data(jsonArr!), name: key))
                    } else {
                        let emptyArr = try? JSONEncoder().encode(value)
                        formData.append(Moya.MultipartFormData(provider: .data(emptyArr), name: key))
                    }
                } else {
                    let valueData = (value as AnyObject).data(using: String.Encoding.utf8.rawValue) ?? Data()
                    formData.append(Moya.MultipartFormData(provider: .data(valueData), name: key))
                }
            }
        }

Multipart seems less forgiving vs. a "Content-Type": "application/json" format.

stale[bot] commented 6 years ago

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

ahmedelmoughazy commented 3 years ago

have you solve it ?