3
votes

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

2
This sounds like an XY Problem. Why do you think you need to do this? What are you trying to do with such a dictionary?Sweeper
i have to send Api request with these kind of parametersAmutha Priya
Then either it's a really badly written API, or you have misunderstood the documentation. Can you give a link to the API docs that tells you that you need a dictionary with 2 of the same keys?Sweeper
It's impossible to do that. Assigning value second to key(y) will replace the previous one.Anis Mansuri
@AmuthaPriya I recently came across a similar API that was accepting URL encoded parameters and multiple values for the same key. Luckily Alamofire can do this just fine. See my answer.gcharita

2 Answers

1
votes

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

1
votes

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)
)