从cURL创建AlamoFire请求

问题描述 投票:1回答:1

我试图将下面的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"

swift alamofire
1个回答
0
投票

你必须在你的身体参数中传递'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代码,这意味着资源已成功创建。

© www.soinside.com 2019 - 2024. All rights reserved.