let parametrs = ["word": newWord.word, "translate": newWord.translate , "studied" :newWord.studied] as [String : Any]
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parametrs, options: []) else { return }
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response
{
print(response)
}
print(parametrs)
guard let data = data else { return }
do
{
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
}
catch
{
print(error)
}
}.resume()
我认为问题与服务器有关
NSHTTPURL响应:0x60000029d240
{网址:http://pavlo-tymoshchuk-inc.right-k-left.com/wordList.json } { 状态代码:200, header { “接受范围”=( 字节 ); 连接 = ( “升级,保持活力” ); “内容长度”=( 6689 ); “内容类型”=( “应用程序/json” ); 日期 = ( “2020 年 2 月 7 日星期五 00:21:32 GMT” ); “保持事件”=( “超时=15,最大值=100” ); “最后修改时间”= ( “2020 年 2 月 6 日星期四 22:55:22 GMT” ); 服务器 = ( Apache ); 升级=( “h2,h2c” ); } } [“word”:“aaa”,“翻译”:[“aaa”],“已学习”:false]
最佳答案
这里没有任何问题。
- 状态 200
- 代码不会测试错误,但我打赌它是零
- 打印响应是您所期望的
- Print JSON 打印 json 对象。
你应该感到高兴。或者你期望什么?
关于ios - URLRequest POST 在 Swift 5 中不起作用。服务器可能有问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60105542/