3

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?

kaushikdr
  • 1,699
  • 2
  • 12
  • 18
  • Possible duplicate of [Alamofire: Sending JSON as request parameter](http://stackoverflow.com/questions/31793155/alamofire-sending-json-as-request-parameter) – cbowns Feb 09 '16 at 15:42
  • Look here: https://github.com/Alamofire/Alamofire#json-encoding – Derek Soike Dec 23 '16 at 00:49

3 Answers3

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.

Gokhan Dilek
  • 4,314
  • 4
  • 21
  • 24
  • yes I did. Just to debug i am sending back the request.data as a response from the server which gives me like { "abc[]" = 3;} – kaushikdr Jul 19 '15 at 09:48
  • So do you want to return the same parameter as dictionary from the server? If that is the case, this sounds like a server side issue. – Gokhan Dilek Jul 19 '15 at 09:54
  • 1
    In my server it checks for a keyword named "abc" in request.data. It doesn't get this keyword. – kaushikdr Jul 19 '15 at 10:00
  • request.data should be like {"abc": [1,2,3]} – kaushikdr Jul 19 '15 at 10:03
  • 1
    Their documentation is horrible. Where can I use the advertised `ParameterEncoding` – William Entriken Jan 21 '16 at 01:20
  • @FullDecent You can use the parameter encoding like this: Almofire.request(.POST, url, parameters: parameters, encoding: .JSON) This is for encoding it to json. You can use as per your need. – kaushikdr Feb 10 '16 at 11:50
0

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]"
    ]
]
Mehmet
  • 3,301
  • 8
  • 26
  • 36
0
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

arman khan
  • 33
  • 6