Send Array of Request Body in an Alamofire Request

Send Array of Request Body in an Alamofire Request

Guys i have fixed the issue. I was playing around with some of the solutions from you guys and suddenly it worked!

So what was the main problem?

  • I changed the request.httpMethod = ".post" to request.httpMethod = "post".

Initially this did not work when i commented back to @NiravD but after some time when i did the changes below, i was able to see the output

Additional changes to view the response on console

  • Replaced .response to .responseJSON

Thanks a lot to everyone who helped :)

Array put request with Alamofire

Alamofire added support for Encodable parameters in Alamofire 5, which provides support for Array parameters. Updating to that version will let you use Array parameters directly. This support should be automatic when passing Array parameters, you just need to make sure to use the version of request using encoder rather than encoding if you're customizing the encoding.

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
}
}

how to send array parameter using alamofire

You just need to define an array, you are almost there, just need an extra set of [] in your dictionary.

let params = ["id": id,
"type_contact":type_contact,
"contacts": [
[
"user_id": user_id,
"firstname": firstname,
"lastname": lastname
]]]

How to pass array of Object parameter to Alamofire swift

let bodyParams: [String : Any] = [
"token": "\(retrivedToken)",
"zipCodes":[
"county": "Washington",
"state": "MN",
"zip_code": "55001",
"city": "Afton"
]
]
}

let urlString = "abc.com"

Alamofire.request(urlString, method: .post, parameters: bodyParams ,encoding: JSONEncoding.default, headers: nil).responseJSON {  
response in
switch response.result {
case .success:
print(response)
break
case .failure(let error):

print(error)
}
}

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

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.

Send an array as a parameter in a Alamofire POST request

AnyObject can only represent class type. In Swift, Array and Dictionary are struct, instead of the class type in many other languages. The struct cannot be described into AnyObject, and this is why Any comes in. Besides of class, Any can be utilized in all other types too, including struct and enum.

Therefore whenever we type cast array or dictionary to AnyObject _TtCs21_SwiftDeferredNSArray error occurs.So we have to Any instead of AnyObject.

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


Related Topics



Leave a reply



Submit