How to send a POST request with nested Dictionary in swift

前端 未结 2 719
花落未央
花落未央 2021-01-15 21:35

I search 100s of posts on StackOverflow but can\'t find any correct answer can anyone help me to solve this mystery.

let param : [String : Any]= [
    \"Serv         


        
相关标签:
2条回答
  • 2021-01-15 22:08

    You can use in this way

        let customerTckt:[String:Any] = ["RequestID" : requestID ,
        "TaskID" : taskID ,
        "Description" : comments ,
        "TicketTypeID" : 3 ,
        "CustomerID" : customerID ,
        "PriorityID" : 3 ,
        "CustomerTPPID" : TTPIDArray ]
    
        let param : [String : Any] = ["ServiceReqID" : 1 ,
                                      "WebUsersID" : customerID,
                                      "FirstName" : userName,
                                      "LastName" : "" ,
                                   "Company":self.profileValues.customerCompanyName ,
                                      "City" : self.profileValues.customerCityName ,
                                      "Email" : self.profileValues.customerEmail ,
                                      "ContactNo" : self.profileValues.customerContactNumber ,
                                      "Country" : "Pakistan" ,
                                      "PackageChange" : 0 ,
                                      "AddressChange" : 0,
                                      "TelInternetVAS" : 0 ,
                                      "Others" : 0 ,
                                      "Comments" : comments ,
                                      "CSAFNO" : self.profileValues.customerCSAFNo,
                                      "SecondaryContactNo" : "" ,
                                      "CustomerTicket" :customerTckt]
    
    
    func postserviceRequestFeedback(url : String , parameter : [String : Any] , tiket : tiket ){
    Alamofire.request(url, method : .post , parameters : parameter , headers : 
    HTTPHeaders).responseJSON { (response) in
        if response.result.isSuccess{}}
    
    0 讨论(0)
  • 2021-01-15 22:28

    Thanks to all, I solve This problem. Here is the step by step solution to this question. 1)

     Alamofire.request(url, method : .post , parameters : parameter , encoding : JSONEncoding.default, headers : tiket ).responseJSON { (response) in
            if response.result.isSuccess{
              let responseJoson : JSON = JSON(response.result.value!)
                print(responseJoson)
             }
    

    encoding is very important here. Don't skip this. Step 2)

     func apiCall(){
      let customerTckt:[String:Any] = ["RequestID" : requestID ,
    "TaskID" : taskID ,
    "Description" : comments ,
    "TicketTypeID" : 3 ,
    "CustomerID" : customerID ,
    "PriorityID" : 3 ,
    "CustomerTPPID" : TTPIDArray ]
    
    let param : [String : Any] = ["ServiceReqID" : 1 ,
                                  "WebUsersID" : customerID,
                                  "FirstName" : userName,
                                  "LastName" : "" ,
                               "Company":self.profileValues.customerCompanyName ,
                                  "City" : self.profileValues.customerCityName ,
                                  "Email" : self.profileValues.customerEmail ,
                                  "ContactNo" : self.profileValues.customerContactNumber ,
                                  "Country" : "Pakistan" ,
                                  "PackageChange" : 0 ,
                                  "AddressChange" : 0,
                                  "TelInternetVAS" : 0 ,
                                  "Others" : 0 ,
                                  "Comments" : comments ,
                                  "CSAFNO" : self.profileValues.customerCSAFNo,
                                  "SecondaryContactNo" : "" ,
                                  "CustomerTicket" :customerTckt]
        let userToken: HTTPHeaders = [
            "Authorization": "bearer \(accessToken)",
            "Content-Type": "application/json"
        ]
       postserviceRequestFeedback(url: postRequestFeedbackUrl, parameter: param , tiket: userToken)
       }
    

    Don't skip "Content-Type" to application/json

    0 讨论(0)
提交回复
热议问题