Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode Swift array for Alamofire post using URLEncoding

Reposting as I messed up with the type of encoding before... I need to send an array to the server with Alamofire using URLEncoding. However, it needs to be encoded in some way for Alamofire to send it properly. This is my code:

let parameters: [String : Any] = [
    "names" : ["bob", "fred"]
]

Alamofire.request(urlString, method: .post, parameters: parameters, encoding: URLEncoding.default)
   .responseJSON { response in
       // etc
   }

However the parameters never get encoded and just get sent as nil. How can I encode it?

like image 320
Tometoyou Avatar asked Jan 31 '23 01:01

Tometoyou


1 Answers

As of now you can use for this & issue on parameters:

let enc = URLEncoding(arrayEncoding: .noBrackets)
Alamofire.request(url, method: .get, parameters: parameters, encoding: enc)
like image 119
Romulo Diniz Avatar answered Feb 05 '23 16:02

Romulo Diniz