Alamofire post request with json encoding

限于喜欢 提交于 2019-12-10 18:09:43

问题


How to send a post request with Alamofire with parameters as json having list of integers i.e, my server expects a dictionary whose value for the key is a list of integers.

I want the parameters as {"abc":[1,2,3]}. How to send this along post request of Alamofire in swift?


回答1:


Have you tried the following?

 var parameter  = ["abc": [1,2,3]]
 Alamofire.request(.POST, "http://www.yoursite.com/api" , parameters:parameter)

I would also look at the documentation over at Alamofire github documentation which is really helpful.




回答2:


an other solution from official documentation.

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

I dont understand how to check your data at the backend on your server but I guess you can check your abc data result like a string [1,2,3]

let parameters = [
    "abc": "[1,2,3]"
    ]
]



回答3:


let parameters = [
    "foo": "bar",
    "baz": ["a", 1],
    "qux": [
        "x": 1,
        "y": 2,
        "z": 3
    ]
]

    Alamofire.request(url,method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON{
                (response) in
                print(response, parameters)
            }

This should work



来源:https://stackoverflow.com/questions/31499735/alamofire-post-request-with-json-encoding

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