Alamofire: syntax for writing POST request with url, method, parameters, headers and encoding
I looked at several previous answers but could not find an up-to-date version that includes ALL of the following parameters: url, method, parameters, encoding, headers.
It:
Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { ... }
Gives error: additional argument "method" when called
UPDATE 26/06/2017
The format of the request is indeed correct, the problem is that the format of one sent parameter was incorrect. The error is quite misleading. See my answer below for a list of the required parameter types and their default values.
source to share
Crystallo's answer is a great way to do this.
In the meantime, I found that the request in my original question actually works, provided that the value passed to the parameter headers is of type [String: String].
The Alamofire error is a little misleading:
Extra argument 'method' in call.
This is why the query you can use is:
Alamofire.request(
url,
method: .post,
parameters: params,
encoding: JSONEncoding.default,
headers: httpHeaders).responseJSON { response in
...
}
With the expected parameter types and their default values ββ(taken from the Alamofire source code):
Alamofire.request(
_ url: URLConvertible,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil)
source to share
The easiest way is to create a specific request and then customize it using the request methods and properties
var request = URLRequest(url: yourUrl)
request.httpMethod = yourMethod
request.setValue(yourCustomizedValue)
request.httpBody = yourBody
...
Alamofire.request(request).responseJSON {...}
source to share