如何通过key =>值发送值作为post方法swift

时间:2017-02-13 11:45:27

标签: java android

我想在key=>value中发送一些像volley库中的参数(如form html)。

我不想将值作为json参数发送。

我该怎么办?

凌空图书馆中的

 Map<String, String>  params = new HashMap<>();
        // the POST parameters:
        params.put("site", "code");
        params.put("network", "tutsplus");

2 个答案:

答案 0 :(得分:0)

您应该使用Dictionary并将字典序列化为JSON并将其设置为HTTPBody

let parameters = ["site":"code", "network":"tutsplus"] as Dictionary<String, String>
let request = URLRequest(url: URL(string:YOURURL)!)
let session = URLSession.shared
request.httpMethod = "POST"

request.httpBody = try! JSONSerialization.data(withJSONObject: parameters, options: [])

替代方案是:

request.httpBody = "site=code&network=tutsplus"

答案 1 :(得分:-1)

您可以在httpBody中设置键和值,如下所示:

request.httpBody = "key=value"

你可以通过添加'&amp;'来添加更多的键,就像你通常在GET中那样做

request.httpBody = "key=value&otherKey=otherValue"