0

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"
            ]
user1960169
  • 3,533
  • 12
  • 39
  • 61
  • Show the declaration of your `parameters` dictionary – Nirav D Jun 14 '17 at 05:30
  • Change the declaration to `[String:Any]` from `[String:String]` means `var dictionary:[String:Any]!` – Nirav D Jun 14 '17 at 05:37
  • do I need to convert that into json as well,, because I tried in rest client it didnt accept when there is [ ] instead of {} – user1960169 Jun 14 '17 at 05:39
  • I'm just saying to change the declaration to `[String:Any]` dictionary from the `[String:String]` dictionary, `[String:Any]` is still a dictionary not the array. – Nirav D Jun 14 '17 at 05:41
  • @user1960169 what is the type for 'method' that you use in Alamofire.request(urlString, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headerToken) ? – Aravind A R Jun 14 '17 at 06:02
  • @AravindAR Its a HTTPMethod – user1960169 Jun 14 '17 at 06:21

4 Answers4

2

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

1

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

Abhijit
  • 41
  • 5
0

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"
                ]
Suryakant Sharma
  • 3,852
  • 1
  • 25
  • 47
  • when I dont want to pass body parameters how can I pass a null or empty value? when it was [String:String] I passed as ["":""] when I dont want to pass those parameters. Can you pplease let me know? – user1960169 Jun 14 '17 at 06:16
  • 1
    if you don't want to send body param create body param as - `let bodyParams : Parameters = [:]` and pass `bodyParams` . You can do the same for 'headerParams' as well. – Suryakant Sharma Jun 14 '17 at 06:21
  • [] nw_endpoint_flow_service_writes [1.1 23.99.116.70:443 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count I am getting this in my log after I changed into [:] this is another method that I dont need to send body params :( – user1960169 Jun 14 '17 at 06:26
  • in response/error are you getting this or this is server side logs? – Suryakant Sharma Jun 14 '17 at 06:34
  • That's great! if my answer helped you, Accept it. Happy Coading. – Suryakant Sharma Jun 14 '17 at 06:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146613/discussion-between-user1960169-and-suryakant). – user1960169 Jun 14 '17 at 08:13
0

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
}
Aravind A R
  • 2,674
  • 1
  • 15
  • 25