Search code examples
swiftalamofire

POST Parameter With Alamofire


hi this is Model of Parameter to POST server :

{"SOSApplicant":{"Vehicles":[{"CarModelID":3299,"ChassisNO":"","Color":"قرمز","VehiclePlateP1":"22","VehiclePlateP2":"الف","VehiclePlateP3":"222","VehiclePlateP4":"22"}],"FirstName":"سینا","LastName":"اکبربان"},"PendingToken":"77398284-dec1-4472-a940-eeb564398d6d"}

I use Alamofire to send post and creat this parameter in let ParamToSend:Parameters = ["SOSApplicant":["FirstName":"\(self.FName)", "LastName":"\(self.LName)","Vehicles":["CarModelID":self.ModelId,"ChassisNO":"\(chasisTextField.text!)","Color":"\(self.carColorTextField.text!)","VehiclePlateP1":"\(firstPelakTextField.text!)","VehiclePlateP2":"\(secondPelakTextField.text!)","VehiclePlateP3":"\(thirdPelakTextField.text!)","VehiclePlateP4":"\(fourthPelakTextField.text!)"]] ,"PendingToken":"\(token)"] but the server send me Error 400 how to creat this parameter??


Solution

  • Your server is expecting an array of Vehicles and you are passing a single Vehicle dictionary.

    Try this.

    let ParamToSend:Parameters = ["SOSApplicant":["FirstName":"\(self.FName)", "LastName":"\(self.LName)",
    "Vehicles":[
        ["CarModelID":self.ModelId,"ChassisNO":"\(chasisTextField.text!)","Color":"\(self.carColorTextField.text!)","VehiclePlateP1":"\(firstPelakTextField.text!)","VehiclePlateP2":"\(secondPelakTextField.text!)","VehiclePlateP3":"\(thirdPelakTextField.text!)","VehiclePlateP4":"\(fourthPelakTextField.text!)"]
    ]] ,"PendingToken":"\(token)"]