Alamofire: Sending JSON as request parameter

纵饮孤独 提交于 2019-11-26 23:41:32

问题


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"
}

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?

Thanks!

Edit:

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.


回答1:


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

Here you have an Dictionary (Dictionary is like a JSON) and also a parameter with another Dictionary(JSON) as value of a parameter...

Is that what you need?



来源:https://stackoverflow.com/questions/31793155/alamofire-sending-json-as-request-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!