当尝试通过 http 向服务器发送 post 数据时,它不返回任何 post 数据。这是一个示例代码:
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()
结果是:
成功转储 0 个帖子变量...没有帖子正文。