簡體   English   中英

如何在Alamofire請求Swift 3中傳遞JSON對象

[英]How to pass JSON object in Alamofire request swift 3

我正在使用Alamofire API進行服務調用。 到目前為止, GET方法運行良好。 現在,我需要執行一個PUT請求。 它也接受這種類型的身體參數。

{
"LeaveEntryCode":0,
"RequestId":0,
"EmployeeCode":17227,
"LeaveYear":2017,
"LeaveTypeCode":1,
"LeaveReasonCode":1,
"BaseType":"ess",
"StartDate":"2017-06-16T00:00:00",
"EndDate":"2017-06-16T00:00:00",
"NoOfDays":1.0,
"StartDateSession":"full",
"EndDateSession":"full",
"PreApproved":false,"ForDate":"1901-01-01T00:00:00",
"Remarks":"I have to attend for a wedding of my close relatives",
"CoveringPersonCode":0,
"RequestStatus":"P",
"Deleted":false,
"Status":false,
"CreatedBy":0,
"CreatedDate":"0001-01-01T00:00:00",
"UpdatedBy":0,
"UpdatedDate":"0001-01-01T00:00:00",
"DeletedBy":0,
"DeletedDate":"0001-01-01T00:00:00",
"ModuleId":2,
"ObjectId":20,
"StartDateString":"06/16/2017",
"EndDateString":"06/16/2017",
"LeaveDayList":["06/16/2017-FH,06/16/2017-SH"],
"SystemLeaveTypeCode":"ANN",
"LeaveTypeName":"ANNUAL",
"Employee":null,
"LieuDayList":null,
"BaseLeaveType":"ANN",
"CoveringPersonName":"",
"LeaveReasonName":"Personal",
"DocumentSource":"LEAVE",
"AttachedDocument":null
}

我創建了一個[String:Any]對象,並將其分配給以下請求中的parameters

但是我在調​​用中遇到了一個名為Extra參數'method'的錯誤。

但是,如果我將其分配為["":""] ,該錯誤就會消失。 我該如何解決? 請幫我。

Alamofire.request(urlString, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headerToken)

UPDATE

var dictionary:[String:String]!
dictionary=[
            "LeaveEntryCode":"0",
            "RequestId":dm.strReqID,
            "EmployeeCode":dm.strEmpCode,
            "LeaveYear":dm.selectedYear,
            "LeaveTypeCode":dm.selectedLeaveTypeCode,
            "BaseType":"ess",
            "StartDate":dm.startDate,
            "EndDate":dm.endDate,
            "NoOfDays":dm.noOFDays,
            "StartDateSession":dm.startDateSession,
            "EndDateSession":dm.endDateSession,
            "RequestStatus":"P",
            "PreApproved":"0",
            "ForDate":"01/01/1901",
            "Remarks":comment,
            "CoveringPersonCode":dm.strcoveringPersonCode,
            "LeaveDayList":strDayLvList,
            "BaseLeaveType":dm.selectedLeaveTypeCode,
            "LeaveReasonCode":dm.selectedReasontypeCode,
            "AttachedDocument":"null"
            ]

您在調用中收到一個稱為Extra參數'method'的錯誤,這是由於標頭引起的,

嘗試將標頭傳遞為nil或如下所示:

//Here param equals to your dictionary as [String :Any]

//Pass Headers as Dictionary as well.

     Alamofire.request("", method: .post, parameters: param, encoding: JSONEncoding.default, headers:["" : ""])

它為我工作。

還要檢查此鏈接: 調用中的Alamofire Swift 3.0 Extra參數

//嘗試此Alamofire.request(urlString, method: method, parameters: parameters as! Parameters, encoding: JSONEncoding.default, headers: headerToken)

參數應該是“ Parameters類型而不是“字典”。

嘗試這個:

let parameters: Parameters = [
                "LeaveEntryCode":"0",
                "RequestId":dm.strReqID,
                "EmployeeCode":dm.strEmpCode,
                "LeaveYear":dm.selectedYear,
                "LeaveTypeCode":dm.selectedLeaveTypeCode,
                "BaseType":"ess",
                "StartDate":dm.startDate,
                "EndDate":dm.endDate,
                "NoOfDays":dm.noOFDays,
                "StartDateSession":dm.startDateSession,
                "EndDateSession":dm.endDateSession,
                "RequestStatus":"P",
                "PreApproved":"0",
                "ForDate":"01/01/1901",
                "Remarks":comment,
                "CoveringPersonCode":dm.strcoveringPersonCode,
                "LeaveDayList":strDayLvList,
                "BaseLeaveType":dm.selectedLeaveTypeCode,
                "LeaveReasonCode":dm.selectedReasontypeCode,
                "AttachedDocument":"null"
                ]

使參數類型為[String:AnyObject]? 並取決於您是否需要參數,是否為其分配值或將其設置為nil 對於標題,使其類型為[String:AnyObject]? 因此,如果沒有標題,請將其nil 例如var dictionary:[String:String]?

if shouldAddParams{
dictionary=[
            "LeaveEntryCode":"0",
            "RequestId":dm.strReqID,
            "EmployeeCode":dm.strEmpCode,
            "LeaveYear":dm.selectedYear,
            "LeaveTypeCode":dm.selectedLeaveTypeCode,
            "BaseType":"ess",
            "StartDate":dm.startDate,
            "EndDate":dm.endDate,
            "NoOfDays":dm.noOFDays,
            "StartDateSession":dm.startDateSession,
            "EndDateSession":dm.endDateSession,
            "RequestStatus":"P",
            "PreApproved":"0",
            "ForDate":"01/01/1901",
            "Remarks":comment,
            "CoveringPersonCode":dm.strcoveringPersonCode,
            "LeaveDayList":strDayLvList,
            "BaseLeaveType":dm.selectedLeaveTypeCode,
            "LeaveReasonCode":dm.selectedReasontypeCode,
            "AttachedDocument":"null"
            ]
} else {
dictionary = nil
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM