Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 344
Amutha Priya Avatar asked Oct 24 '25 16:10

Amutha Priya


1 Answers

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)
)
like image 185
gcharita Avatar answered Oct 26 '25 07:10

gcharita



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!