-1

http://json.org/ defines that JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

I couldn't find a way to make Alamofire work on a post request where the parameters are [Any], which is the format required by the endpoint I'm consuming.

The error message is actualy a bit deceiving because it states "Extra argument in 'method' call" while performing a Alamofire.request but the actual problem is that parameters value is non-conforming.

joycehf
  • 9
  • 2
  • Can you pls post some code? – Sohil R. Memon Jul 05 '19 at 04:40
  • can I see your code that use for it? – Masoud Roosta Jul 05 '19 at 05:02
  • I don't think you can just send [Any] type of parameters. You need to make a key value pair of [String: Any]. So that value can be identified by a Key at the server side. – Aditya Jul 05 '19 at 05:24
  • @Aditya The thing is the server side wasn`t expecting a key value pair, but instead an ordered list of values. Instead of { "numbers": [1,2,3,4] } the server requires [1,2,3,4] which is also a valid json. – joycehf Jul 06 '19 at 11:15

1 Answers1

0

The parameter by the name of parameters in Alamofire expects [String: Any]. If you want to send an Array through Alamofire, you can define custom ParameterEncoding like this

struct CustomEncoding: ParameterEncoding {
    var _httpBody: Data?
    func encode(_ urlRequest: Alamofire.URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
        var request = try URLEncoding().encode(urlRequest, with: parameters)
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = _httpBody
        return request
    }
}

Then use it like this

let body = ... // 'Array' converted to 'Data'
Alamofire.request(...,
                  method: ...,
                  parameters: [:],
                  encoding: CustomEncoding(_httpBody: body),
                  headers: ...)
Malik
  • 3,763
  • 1
  • 22
  • 35