0

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:

  1. 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
  2. Also I have no Idea how to send such thing as Parameters of Alamofire.
  3. 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
A.s.ALI
  • 1,992
  • 3
  • 22
  • 54
  • What do you mean for not able to attach the ApiKey? And about the second problem I think thi is good example how to send paramateres https://stackoverflow.com/a/32013715/1856499 – m1sh0 Nov 19 '18 at 06:11
  • @m1sh0 Simple, I have to send the StudentSubject Array, that I can get from codable, but when I do get Json of StudentSubjectm, then I am unable to attach Apikey in it, Actually I am beginner in iOS , but need to deliver this app, I am android developer, so in Android we use to send JsonArray and JsonObect and write parser, here I learnt about codable, but now I am helpless. – A.s.ALI Nov 19 '18 at 06:15
  • @m1sh0 simple I want to know How to send the array of StudentSubject and Apikey also in json format – A.s.ALI Nov 19 '18 at 06:15
  • okay first you need dict of all object let parameters: [String: Any] = ["ApiKey":"asas-dsadas-xyz-xyz","StudentSubjects": [subjectModel1,sm2...]] and pass this to the Alamofire.request. Alamofire will convert the object as json if you add encoding: JSONEncoding.default – m1sh0 Nov 19 '18 at 06:18
  • please send me in answer, and also how can I send Subjectmodel1, sm2 in list ? – A.s.ALI Nov 19 '18 at 06:28

1 Answers1

0

What you need is to use this method (I'm not sure if you need PUT or POST but it should be POST)

Alamofire.request(.POST, "http://myserver.com", parameters: parameters, encoding: .JSON)
    .responseJSON { request, response, JSON, error in
        print(response)
        print(JSON)
        print(error)
    }

parameters object is

let parameters: [String: AnyObject] = [
    "ApiKey" : "asas-dsadas-xyz-xyz",
    "StudentSubjects" : yourStudnetSubjectsList
]

Alamofire will do the transformation to JSON for you.

m1sh0
  • 2,236
  • 1
  • 16
  • 21
  • can you show me how you generate yourStudnetSubjectsList and what is the error that you have – m1sh0 Nov 19 '18 at 06:31
  • simple, user is entering values in form, then I fill my model values like name etc, its pretty simple – A.s.ALI Nov 19 '18 at 06:35
  • maybe try to create one more class public class RequestModel: Codable { public var ApiKey : String? = "" public var StudentSubjects: [SubjectModel]? = [] } – m1sh0 Nov 19 '18 at 06:53
  • I’m guessing but you can pass it directly, or at lest you can convert it to Jason string with JSONEncoder and send it – m1sh0 Nov 19 '18 at 07:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183872/discussion-between-shararti-kaki-and-m1sh0). – A.s.ALI Nov 19 '18 at 07:08