Extra argument in call Alamofire Swift
you have create simple method.you need to create completion block parameter
try this code
class func checkServerForLogin(_ url:String,email: String, password: String,success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void) {
let parameters = [
"email": email,
"password": password
] as [String : Any]
Alamofire.request(url, method: .post, parameters: parameters).responseString { (response) in
if response.result.isSuccess {
let resJson = JSON(response.result.value!)
success(resJson)
}
if response.result.isFailure {
let error : Error = response.result.error!
failure(error)
}
}
}
AuthService.checkServerForLogin(URL_CHECK_LOGIN, email: email_input, password: password_input, success: { (responseObject) in
print(responseObject)
}) { (error) in
print(error.localizedDescription)
}
Alamofire 4 error request 'extra argument in call'
The encoding
argument of request(...)
expects static
properties, not enum
cases.
We can take a look at the signature of the static request(...)
method of Alamofire
(Alamofire/Source/Alamofire.swift)
public func request(
_ url: URLConvertible,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil)
-> DataRequest
{ /* ... */ }
The first argument is one which conforms to the protocol URLConvertible
, to which String
is valid; all good so far.
The type of the 2nd argument is simply an enum
to which .post
is a valid case, OK.
The third argument should be of type Parameters
, which is a typealias for [String: Any]
/// A dictionary of parameters to apply to a `URLRequest`.
public typealias Parameters = [String: Any]
The argument you supply, pulseNewVote
, is of type Dictionary<String, Array<Dictionary<String, Any>>>
, but should still be able to be consumed by the expected argument type (Dictionary<String, Any>
). So this should still be OK, even if you might want to consider helping Swift out by explicitly type annotating the type of pulseNewVote
to [String: Any]
:
var pulseNewVote: [String: Any] = ...
The fourth argument, the encoding, however, is not an enum
, but a protocol ParameterEncoding
to which a few struct
types conform. These type have static members (returning an instance of self
) which may look like enum
cases when explicitly type, but which must include the explicit typing
public struct JSONEncoding: ParameterEncoding {
// MARK: Properties
/// Returns a `JSONEncoding` instance with default writing options.
public static var `default`: JSONEncoding { return JSONEncoding() }
// ...
}
Hence, you need to replace the value supplied to your fourth argument, .json
, with e.g. JSONEncoding.default
. With this fix, your call to request
should look like:
Alamofire.request(pulseVoteEndpoint, method: .post, parameters: pulseNewVote, encoding: JSONEncoding.default)
The error you're given (additional parameters) come from, however, the completion handler to the reponse
call. The completion handler supplied to a call to response
is one which takes a single DefaultDataResponse
as argument (not 4 different arguments as in your attempt). An instance of the DefaultDataResponse
type have public members request
, response
, data
and error
, which you may access via the single response
parameter (of type DefaultDataResponse
) in the completion handler. Adapting the completion handler in your response
call to this fact (using the example from the Alamofire documentation, we may construct the final request
and response
chained call:
Alamofire.request(pulseVoteEndpoint, method: .post, parameters: pulseNewVote, encoding: JSONEncoding.default)
.response { response in
// note that these are all optionals, so
// you might want to unwrap them first
print(response.request)
print(response.response)
print(response.error)
}
Extra argument 'method' in call Alamofire swift 4
There is something wrong with your AppDelegate.NOTIFICATION_URL
variable in the AppDelegate
.
Because when I removed the key, the piece of code started working.. Hope this will help you find the solution.
func setUpPushNotification(fromDevice: String) {
let title = ""
let url = "something"
let body = "You got a friend request"
let toDeviceID = fromDevice
var headers:HTTPHeaders = HTTPHeaders()
let Method = Alamofire.HTTPMethod.post
headers = ["Content-Type":"application/json","Authorization":"key=1236"]
let notification = ["to":"\(toDeviceID)","notification":["body":body,"title":title,"badge":1,"sound":"default"]] as [String : Any]
Alamofire.request(url as URLConvertible , method: Method, parameters: notification, encoding: JSONEncoding.default, headers: headers)
}
Extra argument in .request call -- Alamofire + Swift
I figured out the issue, no thanks to any documentation or GitHub threads.
The logic in the above code snippet (from the question) referenced a constant called headers
, which was declared outside of getPlaceData(url:parameters:)
. This is because in order to access the Yelp Fusion API you need to include an extra field in the HTTP
header you send to include the private API key using the format mentioned here.
I moved the headers
data structure inside of getPlaceData(url:parameters:)
and it fixed the issue, so my resulting function is as follows:
static func getPlaceData(url: String, parameters: [String: String]) {
let headers: HTTPHeaders = [
"Authorization": "Bearer /*PRIVATE KEY*/"
]
Alamofire.request(url, method: .get, parameters: parameters, encoding:
JSONEncoding.default, headers: headers).responseJSON { (response) in
if response.result.isSuccess {
let json: JSON = JSON(response.result.value!)
print(json)
} else {
print("error")
}
}
}
Not sure why this fixed the issue, but the code works now.
Swift Alamofire Request Error - Extra Argument in call
Try change your request to something like this:
Alamofire.request(searchURL, method: .get, parameters: ["query": term, "region": 2, "lang": "en"], encoding: JSONEncoding.default, headers: nil)
Alamofire post request error Extra argument 'method' in call
your attributes probably aren't valid (if the types don't match Xcode will tell you the error with the closest matching function)
try changing JSONEncoding
to JSONEncoding.default
and that your body
matches [String:Any]
Alamofire.request(String, method: HTTPMethod.post, parameters: [String:Any], encoding: JSONEncoding.default, headers: header)
something along these lines (I don't know about the header attribute I usually have this at nil
)
Alamofire v4 extra argument method in call 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)
Related Topics
Is There Any Particular Use of Closure in Swift? and What's the Benefit
Error: Argument Type Double/String etc. Does Not Conform to Expected Type "Anyobject"
Alamofireimage How to Clean Memory and Cache
Hiding Dividers in Nssplitview
Node.Physicsbody.Joints Downcasting Error
How to Properly Test Against Certain Values in Nseventmodifierflags via Swift
How to Get All Characters of the Font with Ctfontcopycharacterset() in Swift
Contextual Member Has No Associated Value in Swift 3
Value of Optional Type 'Nsurl' Not Unwrapped; Did You Mean to Use '!' or ''
How to Create a Dynamic Link in Firebase Programmatically? Swift
Cannot Use Unarchivefromfile to Set Gamescene in Spritekit
Cannot Read Property 'Keycode' 'Character' Assertion Failure When Detect Modifier Key + Numeric Key
How to Get All Text from a PDF in Swift
"Ambiguous Use of 'Children'" When Trying to Use Nstreecontroller.Arrangedobjects in Swift 3.0