如何在Alamofire请求swift 3中传递JSON对象

时间:2017-06-14 05:28:59

标签: ios json swift3 alamofire put

我正在使用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 argument'method'的错误。

但如果我将其指定为["":""],那么错误就会消失。我怎么解决这个问题?请帮帮我。

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

更新

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"
            ]

4 个答案:

答案 0 :(得分:2)

您收到一个名为Extra argument' 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 parameter in call

答案 1 :(得分:1)

  

//试试这个    Alamofire.request(urlString, method: method, parameters: parameters as! Parameters, encoding: JSONEncoding.default, headers: headerToken)

答案 2 :(得分:0)

参数应该是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"
                ]

答案 3 :(得分:0)

制作[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
}