使用Alamofire发送POST JSON对象时报错:Extra argument in call
Fixing "Extra argument in call" Error with Alamofire POST Request
Hey there! That "Extra argument in call" error pops up because you’re using syntax from an older Alamofire version (like 3.x or earlier) with a newer release of the framework. Alamofire overhauled its API starting from version 4.0, so let’s adjust your code to match the modern syntax.
What’s Wrong with the Original Code?
- Uppercase method enums like
.POSTwere replaced with lowercase variants (.post) encoding: .JSONwas renamed toJSONEncoding.default- The request method’s parameter order and naming shifted to be more consistent
Corrected Code for Alamofire 4.x+
Here’s the updated version that works with recent Alamofire releases:
import Alamofire let parameters = [ "foo": [1,2,3], "bar": [ "baz": "qux" ] ] // Use AF.request (or Alamofire.request) with modern syntax AF.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: JSONEncoding.default) .responseJSON { response in // Handle your response result here switch response.result { case .success(let responseData): print("Request succeeded: \(responseData)") case .failure(let error): print("Request failed: \(error.localizedDescription)") } }
Additional Notes
- For Alamofire 5.x+,
AFis the recommended shorthand alias (thoughAlamofire.requeststill works) JSONEncoding.defaultautomatically sets the correctContent-Type: application/jsonheader, so you don’t need to add it manually- If you absolutely must stick with the old syntax (not recommended—old versions are unmaintained), specify an older Alamofire version in your Podfile:
pod 'Alamofire', '~> 3.0'
内容的提问来源于stack exchange,提问作者Kamal Trivedi
相关产品推荐
相关产品推荐

