Reputation: 300
I need to change how my body is send to my server actually with this code
let parameters: Parameters = [
"users": array
]
print(parameters)
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: ApiSvain.header)
.responseJSON { response in
print(response)
}
Array is [String]
When I print that I got in xcodeDebug:
["users": ["5ab4305e30c73c4aa140aa06", "5ab4324c30c73c4aa140aa07", "5ab432fe30c73c4aa140aa08"]]
But in my backend I got(Node/express):
[ '5ab4305e30c73c4aa140aa06', '5ab4324c30c73c4aa140aa07', '5ab432fe30c73c4aa140aa08' ]
Simple quote and \n between elements, I need to have in backend that result:
"5ab4305e30c73c4aa140aa06", "5ab4324c30c73c4aa140aa07", "5ab432fe30c73c4aa140aa08"]
Upvotes: 0
Views: 466
Reputation: 2120
You can create your own URLRequest and pass it to Alamofire for HTTP Communication.
So if you want to sent array of strings to server as json format. Just create a URLRequest with your URL and set httpBody for Request as serialised array as Data format. This URL request can be directly loaded to Alamofire
Please refer code below.
let array = ["5ab4305e30c73c4aa140aa06", "5ab4324c30c73c4aa140aa07", "5ab432fe30c73c4aa140aa08" ]
var urlRequest = URLRequest.init(url: URL.init(string: "YOUR_URL_STRING")!)
if let data = try? JSONSerialization.data(withJSONObject: array, options: []) {
urlRequest.httpBody = data
}
Alamofire.request(urlRequest)
.responseJSON { response in
print(response)
}
Upvotes: 1