SolvedMoya How to send String JSON Array as parameter?

I want to send String JSON array as parameter, but only dictionary [String:AnyObject] format is supported. How am I supposed to convert JSON array into [String:AnyObject]?

I'm new to Swift language and have experience in Java. I'm using ObjectMapper for JSON mapping.

25 Answers

✔️Accepted Answer

Here is an UNTESTED example that at least compiles.

extention MyService: TargetType {
...
  var parameters: [String:AnyObject]? {
        switch self {
        case .SomeAPI:
            return ["jsonArray": ["Yes", "What", "Abc"]]
        default:
            return nil
        }
    }

  var parameterEncoding: Moya.ParameterEncoding {
        switch self {
        case .patient_Notifications_Create_Endpoint:
            return JsonArrayEncoding.default
        default:
            return JSONEncoding.default
        }
    }
...
}

struct JsonArrayEncoding: Moya.ParameterEncoding {

        public static var `default`: JsonArrayEncoding { return JsonArrayEncoding() }


        /// Creates a URL request by encoding parameters and applying them onto an existing request.
        ///
        /// - parameter urlRequest: The request to have parameters applied.
        /// - parameter parameters: The parameters to apply.
        ///
        /// - throws: An `AFError.parameterEncodingFailed` error if encoding fails.
        ///
        /// - returns: The encoded request.
        public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
            var req = try urlRequest.asURLRequest()
            let json = try JSONSerialization.data(withJSONObject: parameters!["jsonArray"]!, options: JSONSerialization.WritingOptions.prettyPrinted)
            req.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
            req.httpBody = json
            return req
        }

    }

Other Answers:

Here's my solution for sending JSON array to server. Suggestions are welcome as I'm new to Moya.

Define JsonArrayEncoding closure:

let JsonArrayEncodingClosure: (URLRequestConvertible, [String:AnyObject]?) -> (NSMutableURLRequest, NSError?) = { request, data in
    var req = request.URLRequest as NSMutableURLRequest

    do {
        let json = try NSJSONSerialization.dataWithJSONObject(data!["jsonArray"]!, options: NSJSONWritingOptions.PrettyPrinted)
        req.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
        req.HTTPBody = json
    } catch {
        return (req, nil)
    }
    return (req, nil)
}

Configure target :

  var parameters: [String:AnyObject]? {
        switch self {
        case .SomeAPI:
            return ["jsonArray": ["Yes", "What", "Abc"]]
        default:
            return nil
        }
    }

    var parameterEncoding: Moya.ParameterEncoding {
        switch self {
        case .SomeAPI:
            return ParameterEncoding.Custom(JsonArrayEncodingClosure)
        default:
            return ParameterEncoding.JSON
        }
    }

More Issues: