Send array as parameter in Alamofire POST request

Currently, my application uses AWS API Gateway and Alamofire to access various lambda functions that act as my backend.

I need to send an array as one of the parameters to one of these API endpoints, because I use the following code:

var interests : [String] interests = globalInterests.map({ (interest) -> String in return interest.id! }) // Parameters needed by the API let parameters: [String: AnyObject] = [ "name" : name, "lastName" : lastName, "interests" : interests ] // Sends POST request to the AWS API Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON).responseJSON { response in // Process Response switch response.result { case .Success: print("Sucess") case .Failure(let error): print(error) } } 

But this does not work because the array is not recognized by the API, but if I create a β€œstatic” array

 let interests = ["a", "b", "c"] 

Everything works as expected.

How can I fix this situation, given that the array of interests comes from another part of the code, how can I declare or build it?

A friend managed to accomplish this on Android using

 ArrayList<String> 

EDIT:

Printing an array of parameters shows me the following:

 ["name":test, "interests": <_TtCs21_SwiftDeferredNSArray 0x7b05ac00>( 103, 651, 42), "lastName": test] 
+6
source share
5 answers

AnyObject can only represent a class type. In Swift, Array and Dictionary are struct, not a class type in many other languages. A string cannot be described in AnyObject, and that is why Any is included in it. In addition to the Any class, it can be used in all other types, including struct and enum.

Therefore, whenever we enter an array of litas or a dictionary in AnyObject, the error _TtCs21_SwiftDeferredNSArray occurs. Therefore, we should use Any instead of AnyObject.

  let parameters: [String: Any] = [ "name" : name, "lastName" : lastName, "interests" : interests ] 
+3
source

Using NSJSONSerialization to encode JSON, you can create your own NSURLRequest for use in Alamofire, here's an example of Swift 3 :

  //creates the request var request = URLRequest(url: try! "https://api.website.com/request".asURL()) //some header examples request.httpMethod = "POST" request.setValue("Bearer ACCESS_TOKEN_HERE", forHTTPHeaderField: "Authorization") request.setValue("application/json", forHTTPHeaderField: "Accept") //parameter array let values = ["value1", "value2", "value3"] request.httpBody = try! JSONSerialization.data(withJSONObject: values) //now just use the request with Alamofire Alamofire.request(request).responseJSON { response in switch (response.result) { case .success: //success code here case .failure(let error): //failure code here } } } 
+11
source

The problem is that you just declared an array and did not initialize it. This makes the array of interests equal to zero, even if you are trying to insert data. Try to write

 var interests = [String]() 

instead

 var interests : [String] 
+1
source

let ValueArray = ["userid": name, "password": password]

pass the value ValueArray [parameters: ValueArray] also check the encoding accepted by the API.

0
source

This turned out to be a problem with duplicate identifiers in the array. The API code threw an exception that was not sent back as an error.

All other answers are correct, I tested them after finding a problem, and everything worked, so I'm going to vote them.

Many thanks.

0
source

All Articles