Moya/Alamofire - Url Encoded Params with Same Keys

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



Leave a reply



Submit