1

I am using Alamofire in my iOS app. I used post method along with parameters and all went well. Now i want to send a token as header but I am getting an error of Extra argument 'method' in call. I searched for the ways to send header in post request but only found the way I am already trying. What am I doing wrong? Did I miss anything? Here is my code in which I am sending post request.

let urlCreate = "#########"
        Alamofire.request(urlCreate, method: .post, parameters: ["name" : adventureName, "lat" : lat, "long" : long], encoding: JSONEncoding.default, headers: ["jwtToken" : jwtToken]).responseJSON(completionHandler: { response in
            switch response.result {
            case .success:
                print(response)

            case .failure(let error):
                print(error)
                self.errorLabel.text = error as! String
            }

        })

I am using Swift 3, Xcode 8, Alamofire 4

Things i tried:

Clean the project and built again.

Removed the parameter encoding: JSONEncoding.default

Initialized parameters like let paramters = ["name" : adventureName, "lat" : lat, "long" : long] as [String : Any]

Specified the method method: HTTPMethod.post way but still getting the same error.

kinza
  • 535
  • 11
  • 31

2 Answers2

0
let urlCreate = "#########"
let paramters = ["name" : adventureName, "lat" : lat, "long" : long] as [String : Any]
let headers = ["jwtToken" : jwtToken] as [String : String]

        Alamofire.request(urlCreate, method: .post, parameters: paramters, encoding: JSONEncoding.default, headers: headers).responseJSON(completionHandler: { response in
            switch response.result {
            case .success:
                print(response)

            case .failure(let error):
                print(error)
                self.errorLabel.text = error as! String
            }

        })

Note: JSONEncoding.default is not mandatory, try to remove it.

Here is similar ref question: Extra argument 'method' in call of Alamofire

Here is discussion on same issue and resolution by Alamofire developer: https://github.com/Alamofire/Alamofire/issues?utf8=%E2%9C%93&q=extra%20argument

Krunal
  • 77,632
  • 48
  • 245
  • 261
0

I solved this. Header was supposed to be this way.

let urlCreate = ######
        let paramters = ["name" : adventureName, "lat" : lat, "long" : long] as [String : Any]
        let headers: HTTPHeaders = [
            "x-access-token": jwtToken!,
            "Accept": "application/json"
        ]

I got this thing from Alamofire's official docs and its working for me.

kinza
  • 535
  • 11
  • 31