[英]How to pass JSON object in Alamofire request swift 3
I am doing service calls using Alamofire
API. 我正在使用
Alamofire
API进行服务调用。 So far GET
methods are working fine. 到目前为止,
GET
方法运行良好。 And now I need to do a PUT
request. 现在,我需要执行一个
PUT
请求。 Also it is accepting body parameters in this type. 它也接受这种类型的身体参数。
{
"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
}
I created a [String:Any]
object and assigned to parameters
in the following request. 我创建了一个
[String:Any]
对象,并将其分配给以下请求中的parameters
。
But I got an error called Extra argument 'method' in the call.
但是我在调用中遇到了一个名为Extra参数'method'的错误。
But If I assigned it as ["":""]
that error disappears. 但是,如果我将其分配为
["":""]
,该错误就会消失。 How can I solve this? 我该如何解决? Please help me.
请帮我。
Alamofire.request(urlString, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headerToken)
UPDATE 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"
]
You got an error called Extra argument 'method' in call which is due to headers, 您在调用中收到一个称为Extra参数'method'的错误,这是由于标头引起的,
Try passing headers as nil or as follows : 尝试将标头传递为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:["" : ""])
It Worked for me. 它为我工作。
Check this link as well: Alamofire Swift 3.0 Extra parameter in call 还要检查此链接: 调用中的Alamofire Swift 3.0 Extra参数
//尝试此
Alamofire.request(urlString, method: method, parameters: parameters as! Parameters, encoding: JSONEncoding.default, headers: headerToken)
parameters should be of Parameters
type not dictionary. 参数应该是“
Parameters
类型而不是“字典”。
try this: 尝试这个:
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"
]
Make the parameters of type [String:AnyObject]?
使参数类型为
[String:AnyObject]?
and depending on whether you need parameters or not assign value to it or set it as nil
. 并取决于您是否需要参数,是否为其分配值或将其设置为
nil
。 For headers make it of type [String:AnyObject]?
对于标题,使其类型为
[String:AnyObject]?
. 。 So if you don't have header make it
nil
. 因此,如果没有标题,请将其
nil
。 For example var dictionary:[String:String]? 例如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.