如何使用Alamofire从put或post方法发送原始json字符串?
我找不到任何例子。
let params = Mapper().toJSONString(results) // json string with array of objects
Alamofire.request(.PUT, Config().apiGroup, parameters: params)
收到错误:
Cannot convert value of type 'String?' to expected argument type '[String : AnyObject]?'
答案 0 :(得分:4)
Alamofire期待您的错误所说的[String: AnyObject]?
字典,根据您尝试传递数组的代码,您需要将其转换为字典。检查Alamofire中函数request
的签名:
func request(method: Method, _ URLString: URLStringConvertible,
parameters: [String : AnyObject]? = default,
encoding: ParameterEncoding = default,
headers: [String : String]? = default) -> Request
请参阅Alamofire doc:
中的此示例let params = Mapper().toJSONString(results) // json string with array of objects
Alamofire.request(.PUT, "http://httpbin.org/get", parameters: ["params": params])
.response { request, response, data, error in
print(request)
print(response)
print(data)
print(error)
}
我希望这对你有所帮助。