Alamofire v4 extra argument method in call error - ios

Once I updated to alamofire version 4 I get the error: extra argument method in call
Alamofire.request("www.blabla", method: .put, parameters: parameters, headers: headers, encoding: .JSON)
I already changed it to use "method: .put" like above but I still get the error

I had this issue upgrading to Alamofire 4 and solved it by moving the headers argument and making it the last argument in the call. Also encoding: .JSON should be encoding: JSONEncoding.default.
Call should look like this:
Alamofire.request(url: myUrl, method: .put, parameters: myParams,
encoding: JSONEncoding.default, headers: myHeaders)

What type is parameters? It has to be at least [:] - like:
Alamofire.request(url: myUrl, method: .put, parameters: [:], encoding: JSONEnconding.default, headers: myHeaders)

I broke mind into URLRequest and then just the Alamofire call. Nothing I found worked except breaking it up. I'm using Swift 3 and XCode 8.2.1 and I believe this is swift's sourceKit getting an object wrong.
this
Alamofire.request(url:treeURL!, method: .get, parameters: [:], encoding: JSONEncoding.default, headers: ["Authorization" : app.getToken()])
became this:
var request = URLRequest(url: treeURL!)
request.httpMethod = "GET"
request.allHTTPHeaderFields = ["Authorization" : app.getToken()]
Alamofire.request(request as URLRequestConvertible)

Alamofire.request( "http://....", method: .put , parameters: parameters, encoding: JSONEncoding.default).responseJSON{
response in
if response.result.isSuccess {
//some code
}
}

Related

How to use responseDecodable instead of responseJSON using Alamofire 6

One of my apps no longer works due to JSON serialisation failing when using Alamofire.
'responseJSON(queue:dataPreprocessor:emptyResponseCodes:emptyRequestMethods:options:completionHandler:)'
is deprecated: responseJSON deprecated and will be removed in
Alamofire 6. Use responseDecodable instead.
For code with the following lines
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON { response in.. }
When changing to
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:])
.responseDecodable { response in... }
Then I get the error
Generic parameter 'T' could not be inferred
So I add the following
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:])
.responseDecodable(of: ResponseType.self) { response in.. }
I get the error
Cannot find 'ResponseType' in scope
Does anyone have any suggestions?
Unlike .responseJSON which returns a dictionary or array .responseDecodable deserializes the JSON into structs or classes.
You have to create an appropriate model which conforms to Decodable, in your code it's represented by ResponseType(.self).
The associated value of the success case is the root struct of the model.
But deprecated (in a yellow warning) means the API is still operational.
Side note: A JSON dictionary is never [String: Any?] the value is always non-optional.
In Alamofire -> Documentation/Usage.md, I found the definition of the Decodable, so I try like this, then found working.
struct DecodableType: Decodable {
let url: String
}
AF.request(urlString).responseDecodable(of: DecodableType.self) { response in
switch response.result {
case .success(let dTypes):
print(dTypes.url)
case .failure(let error):
print(error)
}
}

Alamofire: syntax for writing a POST request with url, method, parameters, headers and encoding

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.
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)
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 {...}

Pass parameter with json field when invoking POST in Alamofire

I have encountered an issue in swift 3:
I have an API which I need to access for data in my app, but the parameter that it demands is in the following format:
"jsonRequest" = {
"header" : "GetLocationListReq",
"accessKey" : "1234567890abcdefghij"
}//this is in json format.
I tried to pass this parameter as dictionary to call the API, but at that point I obtained this message error:
Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed
Any one knows how I can solve the problem?
I think you want to pass these things in header of the request.
for that you need to do like this
let headers = ["header": "GetLocationListReq",
"accessKey": "1234567890abcdefghij"]
Alamofire.request(url, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON{
r in
//do what you want here
}
hope this will work.
try to post this code
let par:[String:Any] = ["jsonRequest":[
"header" : "GetLocationListReq",
"accessKey" : "1234567890abcdefghij"
]]
pass it like that in Alamofire
Alamofire.request(url, method: .post, parameters: par, encoding: JSONEncoding.default, headers: nil).responseJSON{
r in
//do what you want here
}

Alamofire extra argument 'method' in call

I use Alamofire 4.0.1 and have this code:
let params = Mapper().toJSON(group)
Alamofire.request("\(Config().apiAdminTableGroup)\(group.id)/", method: .put, parameters: params, headers: Config().apiHeaders, encoding: JSONEncoding.default)
.responseJSON { response in
...
}
But getting this error:
Extra argument 'method' in call
This is by documentation, is this bug or?
Check that the structure of your parameters and headers are right, if not the error you mentioned appears. It should look like that:
Alamofire.request("\(Config().apiAdminTableGroup)\(group.id)/", method: .put, parameters: ["param1":"1", "param2":"2"], encoding: JSONEncoding.default, headers: ["Authorization": "Basic xxx"])
try this:
Alamofire.request(.PUT, "\(Config().apiAdminTableGroup)\(group.id)/",
parameters: params).responseJSON { response in
...
}

post application/x-www-form-urlencoded Alamofire

I want to use Alamofire to retrieve a bearer token from Web API but I am new to ios and alamofire. How can I accomplish this with Alamofire?
func executeURLEncodedRequest(url: URL, model: [String : String]?, handler: RequestHandlerProtocol) {
addAuthorizationHeader()
Alamofire.request(.POST,createUrl(url), parameters: model, headers: headers,encoding:.Json)
}
Well you don't really need Alamofire to do this (it can be simply done using a plain NSURLRequest) but here goes:
let headers = [
"Content-Type": "application/x-www-form-urlencoded"
]
let parameters = [
"myParameter": "value"
]
let url = NSURL(string: "https://something.com")!
Alamofire.request(.POST, url, parameters: parameters, headers: headers, encoding: .URLEncodedInURL).response { request, response, data, error in
print(request)
print(response)
print(data)
print(error)
}
I think that the headers can be omitted since alamofire will append the appropriate Content-Type header. Let me know if it works.
You can also find a ton of specification with examples here.
Alamofire 4.7.3 and Swift 4.0 above
As per the documentation for POST Request With URL-Encoded Parameters
let parameters: Parameters = [
"foo": "bar",
"val": 1
]
// All three of these calls are equivalent
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters)
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: URLEncoding.default)
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: URLEncoding.httpBody)
// HTTP body: foo=bar&val=1
Alamofire 5.2
let parameters: [String: [String]] = [
"foo": ["bar"],
"baz": ["a", "b"],
"qux": ["x", "y", "z"]
]
// All three of these calls are equivalent
AF.request("https://httpbin.org/post", method: .post, parameters: parameters)
AF.request("https://httpbin.org/post", method: .post, parameters: parameters, encoder: URLEncodedFormParameterEncoder.default)
AF.request("https://httpbin.org/post", method: .post, parameters: parameters, encoder: URLEncodedFormParameterEncoder(destination: .httpBody))
// HTTP body: "qux[]=x&qux[]=y&qux[]=z&baz[]=a&baz[]=b&foo[]=bar"
Here is example code that should work with Alamofire 4.x and Swift 3.x as of August 2017:
let parameters = [
"myParameter": "value"
]
Alamofire.request("https://something.com", method: .post, parameters: parameters, encoding: URLEncoding()).response { response in
print(response.request)
print(response.response)
print(response.data)
print(response.error)
}
There is no need to set the content-type header explicitly, as it is set by Alamofire automatically.

Resources