Reputation: 229
So Im using an API for an app that helps me store data for events where i send data to the API entered/selected by the user store it and get back an ID to reference to later. Here is some code that Im using.
JSON accepted by API
{
"topic":"test shedule",
"setdate":"21-09-2017",
"scheduledate":"22-09-2017",
"settime":"09:00:00",
"scheduletime":"10:00:00",
"agenda":"hi this is test agenda",
"adminname":"name",
"type":"meeting",
"users": "[{\"category\":\"optional\",\"email\":\"[email protected]\"}]",
"adminemail":"[email protected]"
}
Data Im Sending using Alamofire\
let parameters: Parameters = [
"topic":topicTextField.text!,
"setdate":currentDate as String,
"scheduledate":setDate as String,
"settime":currentTime as String,
"scheduletime":setTime as String,
"agenda":agendaTextField.text!,
"adminname":"abcname",
"type":"meeting",
"users" : "\(smembers)",
"adminemail":"[email protected]"
]
Alamofire.request("\(baseURL)meeting_shedule.php",method: .post, parameters: parameters).responseJSON { response in
if let dict = response.result.value as? Dictionary<String, AnyObject> {
let topic = dict["topic"] as? String
let g_id = dict["g_id"] as? String
self.storeMeeting(topic: topic!, g_id: g_id!)
}
}
smembers is an array created from the dictionary selectedMembers and the dictionary selectedMembers comes from a different ViewController by using prepare for segue
var selectedMembers = [String: String]()
var smembers = [Any]()
var selected = [String: String]()
if selectedMembers.isEmpty == false {
for (key, value) in selectedMembers {
selected = ["email":key, "category":value]
smembers.append(selected)
}
}
The Problem
Im facing a failure response from api. i think the problem is sending "users" to the API as the API is asking for data in string form and Im sending an array i have even tried to solve this problem by converting smembers to jsonstring by serialization but still returns failure.
The api works when Im sending request through post man like this.
I have no clue how to approach this problem, how to send the data that the API will accept.
Thanks for your help
Upvotes: 0
Views: 193
Reputation: 1233
Change logic in as shown bellow
var selectedMembers = [String: String]()
var smembers = [String]()
var selected = ""
if selectedMembers.isEmpty == false {
for (key, value) in selectedMembers {
selected = String(format: "{\"\\email\\\":\"\\%@\"\\,\"\\category\"\\:\"\\%@\"\\}", arguments: [key,value])
smembers.append(selected)
}
}
Then in ur post data
let parameters: Parameters = [
"topic":topicTextField.text!,
"setdate":currentDate as String,
"scheduledate":setDate as String,
"settime":currentTime as String,
"scheduletime":setTime as String,
"agenda":agendaTextField.text!,
"adminname":"abcname",
"type":"meeting",
"users" :"[\(smembers.joined(separator: ","))]",
"adminemail":"[email protected]"
]
Upvotes: 1