Alamofire:将JSON作为请求参数发送 [英] Alamofire: Sending JSON as request parameter
问题描述
我有一个非常长的数组和字符串,我想通过Alamofire发送,尽管我不知道如何发送原始JSON作为参数. JSON看起来像
I have an incredibly long array and string I want to send through Alamofire though I don't know how I would send raw JSON as a parameter. The JSON looks a little like
{
"skus":["8865594-CS,4387296-CS,1175540-CS...."],
"listType": "H"
}
不是让它像Swift数组然后进行序列化一样,有没有一种方法可以将这个JSON作为Alamofire中的参数传递?
Instead of getting that to behave like a Swift array and then serializing, is there a way I can pass this JSON as a parameter in Alamofire?
谢谢!
我能够在文本编辑器中发挥一些魔力来获取以Swift数组样式(如var skus = ["abc", ...]
中一样)格式的参数,因此我将skus
和listType
制成了Dictionary,根据Eric的建议.除我得到一个状态码:414之外,这已经足够好了,这意味着URL太长了.
I was able to pull a bit of magic in a text editor to get the params formatted in the style of a Swift array (as in var skus = ["abc", ...]
) so I made the skus
and listType
into a Dictionary, per Eric's advice. This worked well enough except that I get a status code: 414, meaning the URL is too long.
推荐答案
我不了解Alamofire,但我只是通过Google搜索并在GitHub的自述文件中找到了一些东西.
I don't know Alamofire, but I just googled for it and found something in its ReadMe on GitHub....
let parameters = [
"foo": "bar",
"baz": ["a", 1],
"qux": [
"x": 1,
"y": 2,
"z": 3
]
]
Alamofire.request(.POST, "http://httpbin.org/post", parameters: parameters)
// HTTP body: foo=bar&baz[]=a&baz[]=1&qux[x]=1&qux[y]=2&qux[z]=3
https://github.com/Alamofire/Alamofire
这里有一个字典(字典就像一个JSON),还有一个参数,另一个字典(JSON)作为参数的值...
Here you have an Dictionary (Dictionary is like a JSON) and also a parameter with another Dictionary(JSON) as value of a parameter...
是您需要的吗?
这篇关于Alamofire:将JSON作为请求参数发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!