0

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.

Cole
  • 2,641
  • 1
  • 16
  • 34
  • possible duplicate of [Sending json array via Alamofire](http://stackoverflow.com/questions/27026916/sending-json-array-via-alamofire) – Eric Aya Aug 03 '15 at 17:35
  • @Eric I did check that question but they start out with a native array and do not send another variable. I'm aware I could easily place this in a dictionary and pass it that way, but my objects are JSON, not Swift. – Cole Aug 03 '15 at 17:37
  • Cole, could you give me a code example, Im sure I could help you out :-) – Dennis Weidmann Aug 03 '15 at 17:38
  • @cole Ok, but the accepted answer really seemed to be what you wanted. :) There's also the link that Neo used (http://stackoverflow.com/questions/31499735/alamofire-post-request-with-json-encoding) and many other related ones. – Eric Aya Aug 03 '15 at 17:45
  • @Eric, I decided to try the way you linked since I was able to format my array in a more friendly way and I'm almost there. I've edited my question to reflect this. Thanks!! – Cole Aug 03 '15 at 18:05

1 Answers1

4

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?

Dennis Weidmann
  • 1,942
  • 1
  • 14
  • 16
  • Ok, I can try just pasting the JSON array in as a value of a Dictionary and see how that works. – Cole Aug 03 '15 at 17:39
  • Yes, if that not is what you want, just write me a message, I will help you out – Dennis Weidmann Aug 03 '15 at 17:41
  • 1
    @neo It's better to "flag" a question as Duplicate when answers already exist (your answer is a copy of http://stackoverflow.com/questions/31499735/alamofire-post-request-with-json-encoding and this may well be the right link for this question indeed) – Eric Aya Aug 03 '15 at 17:43
  • Ok Eric, sorry for that.. I had posted my answer almost the same time you have posted your comment... I didn't knew it... – Dennis Weidmann Aug 03 '15 at 17:45
  • You found the right link I think. :) It's just that you should flag the question with it rather than copy/paste answers (people don't like that too much anyway). – Eric Aya Aug 03 '15 at 17:47
  • @Neo. I've decided that the best course of action was to use a dictionary like in your answer. See my question for an update. – Cole Aug 03 '15 at 18:06