Cole
Cole

Reputation: 2646

Alamofire: Sending JSON as request parameter

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.

Upvotes: 0

Views: 8436

Answers (1)

Dennis Weidmann
Dennis Weidmann

Reputation: 1967

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?

Upvotes: 4

Related Questions