正文接收到服务器的null值的HTTP Post请求

时间:2018-12-23 10:10:39

标签: ios json iphone swift api

我正在尝试使用以下代码通过Swift URLRequest将一些数据发布到服务器。

var request = URLRequest(url: URL(string: Global.ip)!)
request.httpMethod = "POST"
request.addValue("application/x-www-form-urlencoded; charset=UTF-8", forHTTPHeaderField: "Content-Type")

let postString = "cmd=getFavorites" + "&ab={\"userId\":\"\(userId)\",\"favId\":\"\(favoriteId)\",\"favoriData\":\(panelData)}&token=\(token)"
let newPostString = postString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
request.httpBody = newPostString?.data(using: .utf8)

let task = URLSession.shared.dataTask(with: request) { data, response, error in

    guard let data = data, error == nil else {
        print("error=\(String(describing: error))")
        completion(false)
        return;
    }
    let json = JSON(data)
    completion(true)
}
task.resume()

0 个答案:

没有答案