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
...
}
Related
let parametersDictionary = [
"email" : "name#gmail.com",
"password" : "password"
]
Alamofire.request("http://nanosoftech.com/store/user_check", method: .post, parameters: (parametersDictionary as NSDictionary) as? Parameters , encoding: JSONEncoding.default, headers: nil).responseJSON { response in
print("response:", response.result.value)
}
I'm working in post method api and above code is not working. I'm getting nil response. But this url is working properly in postman and android studio too. What is the reason behind this issue?
Your url only works when requesting using form with url encoded
Try to use this, as documented on GitHub
Alamofire.request("http://nanosoftech.com/store/user_check", method: .post, parameters: parametersDictionary , encoding: URLEncoding.default)
If this encoding doesn't work, try encoding: URLEncoding.httpBody
Just write look Like bellow.. It is working for me
Alamofire.request("http://era.com.bd/UserSignInSV", method: .post,parameters:["uname":txtUserId.text!,"pass":txtPassword.text!]).responseJSON{(responseData) -> Void in
if((responseData.result.value != nil)){
let jsonData = JSON(responseData.result.value)
}
}
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 {...}
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
}
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
}
}
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.