Moya/Alamofire - URL encoded params with same keys
So I found a solution which is actually pretty simple and obvious.
Reading Alamofire's documentation I found this:
Since there is no published specification for how to encode collection types, Alamofire follows the convention of appending [] to the key for array values (foo[]=1&foo[]=2), and appending the key surrounded by square brackets for nested dictionary values (foo[bar]=baz).
So, for this cases there's Custom ParameterEncoding option which takes closure where you can actually specify your own implementation of how you want parameters to be formed.
Here's the same question with the same answer.
Alamofire: Send multiple URL encoded parameters with same key
It's not possible to create dict like that in iOS, instead, you can do multiple API calls.
Alamofire Passing Parameter With Common Keys and Multiple Values?
You don't need a custom encoding for this format.
You can send parameters encoded like this:
category_name[]=rock&category_name[]=paper
By using URLEncoding
(which you're already doing) and including the multiple values that should have the same key in an array:
let parameters: Parameters = ["category_name": ["rock", "paper"]]
It'll add the []
after category_name
for you, so don't include it when you declare the parameters
.
Moya Alamofire request with no url coding
I solved it by putting code as follows.
Replace task as follows.
public var task: Task {
switch self {
case .getInfo(contactID: _, types: let types):
var arrayOfTypesInString = types.joined(separator: "\", \"")
arrayOfTypesInString = "\"\(arrayOfTypesInString)\""
let params = ["types": "[\(arrayOfTypesInString)]"]
return .requestParameters(
parameters: params,
encoding: URLEncoding.queryString
)
}
}
}
For now, I've done manual JSON
encoding. Alternate way of doing it would be, first convert data to JSON
& from JSON
create string & supply.
Related Topics
Mpmovieplayercontroller Stops Playing After 5 Seconds - Swift
Swift Core Data Sync with Web Server
Get Index of Object from Array of Dictionary with Key Value
How Parsing Firebase Fdatasnapshot JSON Data in Swift
How to Download a PDF File in Swift and Find in File Manager
Saving Already Created Live Photos
Wake Up Application in Background Using Audiosession Like Alarmy iOS App
Change Push Notification Sound File, Only Works After iOS Reboot
Uiimage Aspect Fill When Using Drawinrect
Swift Extension and Enum for Color Schemes
Setting Custom Http Headers in Alamofire in iOS 7 Not Working
Change Ncwidgetdisplaymode Programmatically in iOS10 Widget
Turning on Thread Sanitizer Results in Signal Sigabrt
How to Monitor More Than 20 Regions