How to pass mixed argument type to Alamofire request

344 views Asked by At

I am trying to send a request with mixed type parameter in Alamofire like below:

let params = [
    "page": 1,
    "userdIds": [1,2,3],
    "domain": "github.com"
] as [String : Any]

// Error: Type 'Any' cannot conform to 'Encodable'
return try! try! AF.request(url, parameters: params, encoder: URLEncodedFormParameterEncoder.default)

I am getting an error saying:

Type 'Any' cannot conform to 'Encodable'


Solution:

There is actually solution which can be achieved by using encoding instead of encoder as below:

return try! AF.request(url, parameters: params, encoding: URLEncoding.default)

Problem with solution:

From Documentations: "There are additional methods that allow you to make requests using Parameters dictionaries and ParameterEncoding types. This API is no longer recommended and will eventually be deprecated and removed from Alamofire." As documentation mentions it will be removed.

Question:

Is there any way to resolve this problem without using soon-to-be deprecated method I have mentioned above?

1

There are 1 answers

0
Jon Shier On BEST ANSWER

Use an Encodable type:

struct Parameters: Encodable {
  let page: Int
  let userIDs: [Int]
  let domain: String
}