我试图将下面的groupMe alamofire post请求转换为Swift中适当的Alamofire格式。特别困惑的是转换cURL的-d
部分。
$ curl -X POST -H "Content-Type: application/json" -d '{"name": "Family"}' https://api.groupme.com/v3/groups?token=YOUR_ACCESS_TOKEN
这就是我所拥有的:
let param = ["source_guid": "GUID", "text": "test message"] as Dictionary<String, String>
Alamofire.request(groupMePostUrl, method: .post, parameters: param, encoding: JSONEncoding.default, headers: ["Content-Type": "application/json"]).responseJSON { response in
}
运行这个,我在调试器中得到消息"text is required"
。
你必须在你的身体参数中传递'name'。可能你可以做这样的事情:
let param = ["name": "Family", "text": "test message"]
Alamofire.request("https://api.groupme.com/v3/groups?token=YOUR_ACCESS_TOKEN", method: .post, parameters: param, encoding: JSONEncoding.default, headers: ["Content-Type": "application/json"]).responseString { (responseStr) in
print("response :\(responseStr)")
}
使用它,您将获得201代码,这意味着资源已成功创建。