从cURL创建AlamoFire请求

时间:2018-03-01 06:07:45

标签: swift alamofire

我正在尝试将下面的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"

1 个答案:

答案 0 :(得分:0)

你必须通过&#39; name&#39;在你的身体参数。也许你可以这样做:

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代码,这意味着资源已成功创建