user1960169
user1960169

Reputation: 3653

How to pass JSON object in Alamofire request swift 3

I am doing service calls using Alamofire API. So far GET methods are working fine. And now I need to do a PUT request. 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.

But I got an error called Extra argument 'method' in the call.

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

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

Upvotes: 0

Views: 1016

Answers (4)

Aravind A R
Aravind A R

Reputation: 2714

Make the parameters of type [String:AnyObject]? and depending on whether you need parameters or not assign value to it or set it as nil. For headers make it of type [String:AnyObject]?. So if you don't have header make it nil. For example 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
}

Upvotes: 0

Abhijit
Abhijit

Reputation: 41

//try this Alamofire.request(urlString, method: method, parameters: parameters as! Parameters, encoding: JSONEncoding.default, headers: headerToken)

Upvotes: 1

Manisha Joshi
Manisha Joshi

Reputation: 51

You got an error called Extra argument 'method' in call which is due to headers,

Try passing headers as nil or as follows :

//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

Upvotes: 2

Suryakant Sharma
Suryakant Sharma

Reputation: 3960

parameters should be of Parameters type not dictionary.

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

Upvotes: 0

Related Questions