3

I've had a look at tons of previous answers, but couldn't find an up-to-date one that includes ALL the following parameters: url, method, parameters, encoding, headers.

This:

Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { ... }

Gives the error: Extra argument "method" in call


UPDATE 26/06/2017

The format of the request is actually correct, the issue is that the format of one parameter sent was incorrect. The error is pretty misleading. See my answer below for a list of the parameter's types required and their default value.

Kqtr
  • 5,824
  • 3
  • 25
  • 32
  • check this answers https://stackoverflow.com/questions/44484772/how-to-post-nested-json-by-swiftyjson-and-alamofire/44500753#44500753 and https://stackoverflow.com/questions/44639529/using-manager-request-with-post/44639768#44639768 – Reinier Melian Jun 22 '17 at 15:22

2 Answers2

2

Cristallo's answer is a great custom way to do it.

In the meantime, I've discovered that the request in my original question actually works, at the condition that the value passed to the parameter headers is of type [String: String].

Alamofire's error is a bit misleading:

Extra argument 'method' in call.

Here is therefore the request that can be used:

Alamofire.request(
        url,
        method: .post,
        parameters: params,
        encoding: JSONEncoding.default,
        headers: httpHeaders).responseJSON { response in
        ...
    }

With the parameters types expected and their default values (taken from Alamofire source code):

Alamofire.request(
    _ url: URLConvertible,
    method: HTTPMethod = .get,
    parameters: Parameters? = nil,
    encoding: ParameterEncoding = URLEncoding.default,
    headers: HTTPHeaders? = nil)
Kqtr
  • 5,824
  • 3
  • 25
  • 32
1

the easiest way is to create a specific request and then customize it using Request methods and properties

var request = URLRequest(url: yourUrl)
request.httpMethod = yourMethod 
request.setValue(yourCustomizedValue)
request.httpBody = yourBody
...

Alamofire.request(request).responseJSON {...} 
cristallo
  • 1,951
  • 2
  • 25
  • 42