Search code examples
swiftalamofire

Send array of objects in alamofire


I need to to send a list of objects to an end point. the required type has the following keys

{
  id: Int;
  all: Bool;
  batch: Bool;
  chapter: Bool;
  myNetwork: Bool;
  categoryId: Int;
}

the api expected a list of the above object. i generated a list using Parameter type of Alamofire like this.

[
    ["chapter": "true", "myNetwork": "false", "id": "3", "categoryId": "1", "memberId": "13", "batch": "true", "all": "true"], 
    ["categoryId": "2", "batch": "false", "myNetwork": "true", "all": "true", "id": "891", "memberId": "13", "chapter": "true"], 
    ["batch": "false", "memberId": "13", "categoryId": "3", "all": "false", "id": "1779", "myNetwork": "false", "chapter": "false"], 
    ["batch": "true", "id": "2667", "all": "false", "chapter": "true", "memberId": "13", "categoryId": "4", "myNetwork": "false"]
]

but it shows error

Cannot convert value of type '[[String : Any]]' to expected argument type 'Parameters?' (aka 'Optional>')

How can i sent this list of objects to an API using Alamofire ? the code that dispatch request

Alamofire.request("(APIManager.url)/Privacy/Update", method: .post, parameters: params, encoding: JSONEncoding.default, headers: APIManager.headers)


Solution

  • your parameters should be like this

    ["key": "value", "key": "value"]
    

    which is a dictionary, what you're using is an array of dictionary that's why you're getting an error