当尝试通过 http 向服务器发送发布数据时,它没有返回任何发布数据。这是一个示例代码:
var request = URLRequest(url: URL(string: "http://posttestserver.com/post.php")!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let json = "{\"key\":\"c7cbdc09820372\",\"rand\": \"13baa5274c2b107727\"}"
request.httpBody = json.data(using: .utf8)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if data != nil, let result = String(data: data!, encoding: .utf8) {
print("\(result)")
}
}.resume()
结果是:
Successfully dumped 0 post variables... No Post body.
最佳答案
将你的json字符串转换成字典然后
jsonData = 尝试? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
request.httpBody = jsonData
关于json - Swift http post 没有发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43944774/