0

I need to send a list of strings under a key in the body. Alamofire makes parameters super simple but I can't figure out how to do it in the body of the request. This doesn't help me since its for a simple string and I can't figure out how to make it work for an array of strings: POST request with a simple string in body with Alamofire . This one is titled about a JSON body but the answer is giving them as params Alamofire 4, Swift 3 and building a json body . Does anyone have an answer or a link to something that actually solves my problem?

Code as requested:

var params = ["phone_numbers": [6314560046, 8458200476]] as [String: Any]

Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON() { response in
Community
  • 1
  • 1
Tommy K
  • 1,487
  • 2
  • 24
  • 46
  • Actually you need to create a request object and then set the http body. URLRequest contains this property. Use `let r = Almofire.request("API_CALL")` and `r.httpBody = "YOUR_KEY_VALUE_IN_STRING"` – Sachin Vas Feb 14 '17 at 06:19

1 Answers1

0

With Alamofire, the parameters are the body of the request. This creates your key with value of array of strings:

let parameters = ["Key": ["String1", "String2", "String3"]]

Then use the parameters in making the request:

    Alamofire.request(URL, method: .post, parameters: paramaters, encoding: JSONEncoding.default)
.responseJSON { response in
    print(response)
}
  • This isn't working for me, I do this exact thing and when I see the call on the backend server the params are sent as URL params, not in the body. I am very confused – Tommy K Feb 14 '17 at 05:58
  • can you post some code? It sound like perhaps urlEncoding rather than JSONEncoding is being used, hard to tell. I'd be happy to help you work through it – Jeremy Moore Feb 14 '17 at 06:38