I need to do this in my project:
I can do this easily if I manually append strings to my URL in Alamofire, but I don't want that. I want the parameters as Parameter object.
multiple values in one common key for parameter

What I've been doing:
public func findCreate(tags: [String], withBlock completion: @escaping FindCreateServiceCallBack) {
    
    /* http://baseurlsample.com/v1/categories/create_multiple?category_name[]=fff&category_name[]=sss */
    
    let findCreateEndpoint = CoreService.Endpoint.FindMultipleCategories
    
    let parameters: Parameters = ["category_name[]" : tags]
    
    Alamofire.request(
        findCreateEndpoint,
        method: .post,
        parameters: parameters,
        encoding: URLEncoding(destination: .queryString),
        headers: nil
        ).responseJSON { (response) in
            print(response)
    }
//....
}
The current result if I run this is okay but the values sent to the server has [" "]. For example:
["chocolate"]
Again, the questions are, in which part of my whole code I'm wrong? How can I send parameters like the above that have one common key and multiple values?
I also have tried adding encoding option to the Alamofire.request() If I add encoding: JSONEncoding.prettyPrinted or encoding: JSONEncoding.default I get status code 500.
Some links that have the same question but no exact answers, I always see posts that have answers like using a custom encoding and that's it.
https://github.com/Alamofire/Alamofire/issues/570
Moya/Alamofire - URL encoded params with same keys
Additional info:
This works, but I need to send multiple String:
let parameters: [String : Any] = ["category_name[]" : tags.first!]
And this works as well:
Alamofire.request("http://baseurlsample.com/v1/categories/create_multiple?category_name[]=fff&category_name[]=sss", method: .post).responseJSON { (data) in
    print(data)
}
                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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With