Sending json array via Alamofire
You can just encode the JSON with NSJSONSerialization
and then build the NSURLRequest
yourself. For example, in Swift 3:
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let values = ["06786984572365", "06644857247565", "06649998782227"]
request.httpBody = try! JSONSerialization.data(withJSONObject: values)
AF.request(request) // Or `Alamofire.request(request)` in prior versions of Alamofire
.responseJSON { response in
switch response.result {
case .failure(let error):
print(error)
if let data = response.data, let responseString = String(data: data, encoding: .utf8) {
print(responseString)
}
case .success(let responseObject):
print(responseObject)
}
}
For Swift 2, see previous revision of this answer.
post array in Alamofire
var request = URLRequest(url: try! "your api".asURL())
request.httpMethod = "POST"
let arrParam = ["abc", "xyz"]
request.httpBody = try! JSONSerialization.data(withJSONObject: arrParam)
Alamofire.request(request).responseJSON { response in
switch (response.result) {
case .success:
//success code here
case .failure(let error):
//failure code here
}
}
Send array of objects in alamofire
your parameters should be like this
["key": "value", "key": "value"]
which is a dictionary, what you're using is an array of dictionary that's why you're getting an error
Related Topics
Shall We Always Use [Unowned Self] Inside Closure in Swift
Why Can't a Get-Only Property Requirement in a Protocol Be Satisfied by a Property Which Conforms
How to Deal With @Objc Inference Deprecation With #Selector() in Swift 4
Precision String Format Specifier in Swift
"Classname Has No Member Functionname" When Adding Uibutton Target
How to Multiply Two Arrays Element-Wise
Swift - Validating Uitextfield
How to Create a Reference Cycle Using Dispatchqueues
How to Parse Json With Decodable Protocol When Property Types Might Change from Int to String
Selecting Global or Object Print Function
Difference Between Functions and Closures
Xcode 8/Swift 3: Simple Uipicker Code Not Working
Separating Multiple If Conditions With Commas in Swift
How to Avoid Force Unwrapping a Variable
Swiftui 2: the Way to Open View in New Window