Reputation: 632
Can't figure out how to pass date to the server with Alamofire .post method. I have to form JSON body like this:
{
"title": "My Title",
"locations": [
{
"location": "locationID"
}
],
}
I'm stuck on "locations" property. Probably, it has to be an Array of objects with one location property which is a string type. For this moment my code is:
@IBAction func createEvent(_ sender: Any) {
let parameters: Parameters = [
"title": Event.title ?? nil,
"locations": //What have I wright here?
]
Alamofire.request(requestURL,
method: .post,
parameters: parameters,
encoding: JSONEncoding.default,
headers: headers).responseJSON {response in
switch response.result {
case .success:
print(response)
}
case .failure(let error):
print(error)
}
}
}
Please help.
Upvotes: 0
Views: 1379
Reputation: 527
you can try with
let parameters: Parameters = [
"title": Event.title ?? nil,
"locations": [dictionary]]
]
Upvotes: 1
Reputation: 1418
if your API request accepts particular string formate then you need to convert Date in String formate with DateFormatter or else if that accept Date Object then you pass but Date object but that's maybe not possible. so please try with first option Convert date in string format which is predecided by server parameter.
or if possible then share request parameter type support on your server.
Upvotes: 0
Reputation: 100503
You can try
let parameters: Parameters = [
"title": Event.title ?? nil,
"locations": [["location":"idherrr"]]
]
Upvotes: 2