问题
I am really struggling to set body parameter for posting data to server. I have different json encoding references but its not working. Anybody please suggest.
Issue is after passing values into parameter SectionList model gives backslashes and over all parameters not serialized.
My values are :
- AppID
- ApprovDT
- UnitId
- SectionList (Model as I encode it to JSON).
Variable Code:
let someDate = Date()
let approvDT = Int(someDate.millisecondsSince1970)
let unitId = id
let AppID = UserDefaults.standard.integer(forKey: "appointmentID")
//allSectionList is the model and converting to JSON
let jsonData = try! JSONEncoder().encode(allSectionList)
let jsonString = String(data: jsonData, encoding: .utf8)!
print(jsonString)
I want to create this Post request already using Codable Model :
{"appointmentId":15473352,"approveDateTime":1575463820102,"projectUnitId":8550749,"sectionList":[{"items":[{"actionId":4,"actionType":101,"required":true,"selection":[{"itemValue":"3","required":false,"textField":"Sayaç bodrumda"}],"textField":"Sayaç Yeri","textValue":"Sayaç bodrumda"}],"title":"Proje Bilgileri"}]}
Model { "projectUnitId": 0, "appointmentId": 0, "approveDateTime": "0001-01-01T00:00:00", "sectionList": null }
Alamofire:
let parameters: [String: Any] = [
"projectUnitId" : id ?? 0,
"appointmentId" : AppID,
"approveDateTime" : approvDT,
"sectionList": jsonString as String
]
print(parameters)
Alamofire.request(urlString, method: .post, parameters: parameters ,encoding: JSONEncoding.default, headers: [ "Accept":"application/json", "Authorization":"Bearer \(Common.TOKEN_KEY)"]).responseJSON {
response in
switch response.result {
case .success:
print(response)
break
case .failure(let error):
print(error)
}
}
回答1:
Model
struct Model: Codable {
enum CodingKeys: String, CodingKey {
case approveDateTime
case appointmentId
case projectUnitId
case sectionList
}
var approveDateTime: Int?
var appointmentId: Int?
var projectUnitId: Int?
var sectionList: [SectionList]?
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
approveDateTime = try container.decodeIfPresent(Int.self, forKey: .approveDateTime)
appointmentId = try container.decodeIfPresent(Int.self, forKey: .appointmentId)
projectUnitId = try container.decodeIfPresent(Int.self, forKey: .projectUnitId)
sectionList = try container.decodeIfPresent([SectionList].self, forKey: .sectionList)
}
}
struct SectionList: Codable {
enum CodingKeys: String, CodingKey {
case title
case items
}
var title: String?
var items: [Items]?
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
title = try container.decodeIfPresent(String.self, forKey: .title)
items = try container.decodeIfPresent([Items].self, forKey: .items)
}
}
struct Items: Codable {
enum CodingKeys: String, CodingKey {
case selection
case textField
case actionType
case actionId
case textValue
case required
}
var selection: [Selection]?
var textField: String?
var actionType: Int?
var actionId: Int?
var textValue: String?
var required: Bool?
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
selection = try container.decodeIfPresent([Selection].self, forKey: .selection)
textField = try container.decodeIfPresent(String.self, forKey: .textField)
actionType = try container.decodeIfPresent(Int.self, forKey: .actionType)
actionId = try container.decodeIfPresent(Int.self, forKey: .actionId)
textValue = try container.decodeIfPresent(String.self, forKey: .textValue)
required = try container.decodeIfPresent(Bool.self, forKey: .required)
}
}
Encode Model and pass it as parameter in request.
func params(model:Model) -> [String: Any] {
do {
let data = try JSONEncoder().encode(model)
guard let jsonObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] else { return [:] }
return jsonObject
} catch {
print("ERROR:\(error) ")
}
return [:]
}
Alamofire.request(urlString, method: .post, parameters:params(),encoding: JSONEncoding.default, headers: [ "Accept":"application/json", "Authorization":"Bearer \(Common.TOKEN_KEY)"]).responseJSON {
response in
switch response.result {
case .success:
print(response)
break
case .failure(let error):
print(error)
}
}
回答2:
I solved Issue by adding String extension.
来源:https://stackoverflow.com/questions/59194884/how-to-set-body-parameter-for-alamofire-swift-parameters-issue