首页 > 解决方案 > 在 Swift 中使用 Alamofire 调用的额外参数“方法”

问题描述

我正在尝试向 Alamofire 发出请求,但错误发生在 parameterer 中method: HTTPMethod,我使用了提示性参数.post

Alamofire.request(OdooAuth.host2!, method: .post, parameters: [String:Any], encoding: JSONEncoding.default, headers: [])

主机没问题,因为如果我尝试其他方式,例如:

AlamofireXMLRPC.request(OdooAuth.host2!, methodName: "execute_kw", parameters: params) 这行得通。

问题是我想使用 JSON 而不是 XML。

XCode 中的错误:

在此处输入图像描述

我在网上、Github、Stackoverflow 上搜索了很多帖子,但类似的问题没有答案或没有解决我的问题。

标签: iosswiftalamofire

解决方案


标头应该是字典[:]而不是数组[],这就是方法签名不匹配的原因。

尝试

let headers = [
    "Content-Type": "application/json",
    "Accept": "application/json"
]

Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { response in 
    // handle the response here
}

推荐阅读