kaushikdr
kaushikdr

Reputation: 2019

Alamofire post request with json encoding

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?

Upvotes: 3

Views: 4439

Answers (3)

arman khan
arman khan

Reputation: 33

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

Upvotes: 0

Mehmet
Mehmet

Reputation: 3347

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

Upvotes: 0

Gokhan Dilek
Gokhan Dilek

Reputation: 4402

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.

Upvotes: 1

Related Questions