如何快速发送带有嵌套字典的POST请求 [英] How to send a POST request with nested Dictionary in swift

查看:301
本文介绍了如何快速发送带有嵌套字典的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StackOverflow上搜索了上百个帖子,但找不到正确答案,有人可以帮助我解决这个难题。

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]= [
    "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" : [
        "RequestID" : requestID,
        "TaskID" : taskID,
        "Description" : comments,
        "TicketTypeID" : 3,
        "CustomerID" : customerID,
        "PriorityID" : 3,
        "CustomerTPPID" : TTPIDArray
    ]
]

仅将带有此代码的第一个模型发送到服务器,并使用blanck值传递secound模型通行证

with this code ony first model is sent to server and secound model pass with blanck values

func postserviceRequestFeedback (
    url : String,
    parameter : [String : Any],
    tiket : HTTPHeaders
)
{
    Alamofire.request (
        url,
        method : .post,
        parameters : parameter,
        headers : tiket
    )
    .responseJSON { (response) in     if response.result.isSuccess{    } }

请帮助我。

推荐答案

感谢所有人,
我解决了这个问题。
这是此问题的分步解决方案。
1)

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)
         }

编码在这里非常重要。不要跳过这个。
步骤2)

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)
   }

不要将 Content-Type跳过到application / json

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

这篇关于如何快速发送带有嵌套字典的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆