Alamofire 使用通用键和多个值传递参数? [英] Alamofire Passing Parameter With Common Keys and Multiple Values?
问题描述
我需要在我的项目中这样做:
如果我在 Alamofire 中手动将字符串附加到我的 URL,我可以轻松地做到这一点,但我不想要那样.我希望参数作为 Parameter 对象.
参数的一个公用键中的多个值
我一直在做什么:
public func findCreate(tags: [String], withBlock completion: @escaping FindCreateServiceCallBack) {/* http://baseurlsample.com/v1/categories/create_multiple?category_name[]=fff&category_name[]=sss */让 findCreateEndpoint = CoreService.Endpoint.FindMultipleCategories让参数:参数 = [category_name[]"";: 标签]Alamofire.request(找到创建端点,方法:.post,参数:参数,编码:URLEncoding(目的地:.queryString),标头:无).responseJSON {(响应)中打印(响应)}//....}
如果我运行它,当前结果是可以的,但是发送到服务器的值有 [";"].例如:
[巧克力"]
再次,问题是,我整个代码的哪一部分是错误的?如何发送具有一个公共键和多个值的上述参数?
我也尝试将 encoding 选项添加到 Alamofire.request() 如果我添加 encoding: JSONEncoding.prettyPrinted
或 编码:JSONEncoding.default
我得到状态码 500.
一些具有相同问题但没有确切答案的链接,我总是看到有使用自定义编码等答案的帖子,仅此而已.
附加信息:
这有效,但我需要发送多个字符串:
let 参数:[String : Any] = [category_name[]";:标签.首先!]
这也有效:
Alamofire.request("http://baseurlsample.com/v1/categories/create_multiple?category_name[]=fff&category_name[]=sss", method: .post).responseJSON { (data)在打印(数据)}
您不需要为此格式自定义编码.
您可以发送这样编码的参数:
category_name[]=rock&category_name[]=paper
通过使用 URLEncoding
(您已经在这样做)并在数组中包含应该具有相同键的多个值:
let 参数:Parameters = ["category_name": ["rock", "paper"]]
它会在category_name
之后为你添加[]
,所以当你声明参数
时不要包含它.>
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.
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
.
这篇关于Alamofire 使用通用键和多个值传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!