I have to send request to Server. Saving user record. The user record contains the model of Subject, and that Subject contains multiple model and arrays also. let me give you little example.
@ignore
public class SubjectModel: Codable {
public var Name : String? = ""
public var Price : Int? = 0
public var ISBN : String? = ""
public var listTeachers : [TeacherModel]? = []
}
public class TeacherModel: Codable {
public var Name : String? = ""
public var PhoneNo : String? = ""
public var listClasses: [ClassModel]? = []
}
public class ClassModel: Codable {
public var Name : String? = ""
public var Section : String? = ""
public var RoomNo : String? = ""
}
So I have to send the SubjectModel, and as you can see the SubjectModel contains multiple Arrays of InnerModel. All models are codable.
So the JSON that my webservice required must look like following
{ApiKey:"asas-dsadas-xyz-xyz","StudentSubjects":{Now here come arrays,"InnerList":[here comes inner arrays ]}}
Problems:
- As you can see I have Implemented Codables to my model, so I am getting good and expected Json, But I am not able to attach the Api Key
- Also I have no Idea how to send such thing as Parameters of Alamofire.
- Or You can say it simply like"How to send the StudentSubject and Apikey also in json format"
Please help me, I am bashing my head but not able to get the reply on server side, infact I am getting null.
UPDATE1: This is how I am doing it
let jsonEncoder = JSONEncoder()
let jsonData = try jsonEncoder.encode(mySubjectModel)
let json = String(data:jsonData,encoding:String.Encoding.utf8)
let parameter: [String:AnyObject] = [
"StudentSubjects" : json as AnyObject,
"ApiKey":"xyz-zyz-zyz"
]
And then Sending it like this
Alamofire.request(Common.URL+ServiceMethodName, method: .post, parameters:parameter, encoding: JSONEncoding.default, headers: nil) .responseJSON { response in