Alamofire json encoded post request

How do I send a post request using Alamofire with parameters as json having a list of integers. ie, my server is expecting a dictionary whose value for the key is a list of integers.

I need parameters like {"abc": [1,2,3]}. How to mail this Alamofire request in quick?

+3


source to share


3 answers


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 Alamofire github documentation , which is really helpful.

+1


source


another solution from the 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 don't understand how to validate your server data on your server, but I think you can validate your abc result as string [1,2,3]

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

      

0


source


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

0


source







All Articles