You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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 .POST were replaced with lowercase variants (.post)
  • encoding: .JSON was renamed to JSONEncoding.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+, AF is the recommended shorthand alias (though Alamofire.request still works)
  • JSONEncoding.default automatically sets the correct Content-Type: application/json header, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:18:50