Amutha Priya
Amutha Priya

Reputation: 341

Alamofire: Send multiple URL encoded parameters with same key

I wanna give Alamofire request with the following

var postParameters=[
    "x" : "value1",
    "y" : "value2",
    "y" : "value3"
]

How do I achieve this?.

I can't give it as any collection. I am in need to give it as a separate parameters only.

Any hint will be helpful for me. Thanks in advance.

enter image description here

Upvotes: 4

Views: 1491

Answers (2)

gcharita
gcharita

Reputation: 8327

While there is no possible way of creating a dictionary with duplicate keys, there is a way to pass these kind of parameters to your server by using an array:

let postParameters: [String: Any] = [
    "x" : "value1",
    "y" : ["value2", "value3"]
]

then when you call request function of Alamofire, you pass a new instance of URLEncoding as encoding parameter and specify the way that you want to encode arrays, like this:

AF.request(
    "url",
    method: .post,
    parameters: postParameters,
    encoding: URLEncoding(arrayEncoding: .noBrackets)
)

Upvotes: 1

komara
komara

Reputation: 382

It's not possible to create dict like that in iOS, instead, you can do multiple API calls.

Upvotes: 1

Related Questions