gpt4 book ai didi

ios - http请求删除和放置

转载 作者:搜寻专家 更新时间:2023-10-31 22:18:07 24 4
gpt4 key购买 nike

下面是我获取和发布 HTTP 请求的代码。我想知道的是如何对 delete 和 put 进行 HTTP 请求。我已经让 get 和 post 工作成为可能。我想知道如何根据下面的代码删除和放置。当我想将其更改为删除时,我的邮政编码要更改什么?缺少什么?我想删除使用 ID例如“id”:16,

let parameters = ["name": "test", "desc": "test" , "reward":"1.00" , "sched":"2018-04-05T11:49:51+08:00",  "occurrence":["name": "once"]
, "status": "created", "created_by": "test@gmail.com","created_for": "test.com"] as [String : Any]

guard let url = URL(string: "http://test.tesst.eu:8000/api/v1/test/") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
request.httpBody = httpBody

let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}

if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}.resume()

得到

guard let url = URL(string: "http://test.test:8000/api/v1/test") else { return }

let session = URLSession.shared
session.dataTask(with: url) { (data, response, error) in
if let response = response {
print(response)
}

if let data = data {
print(data)
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}.resume()

最佳答案

唯一不同的是 GET 参数在 url 本身中,POST & DELETE & PUT 的工作方式相同,只需更改 httpMethod 参数并指定 httpBody 如果你想,意思是

DELETE : 表示从特定的 url 中删除资源

PUT : 将资源放入网络服务器

//

let parameters = ["ID": "16"] as [String : Any]
guard let url = URL(string: "http://test.tesst.eu:8000/api/v1/test/") else { return }
var request = URLRequest(url: url)
request.httpMethod = "DELETE"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject:parameters, options: []) else { return }
request.httpBody = httpBody

关于ios - http请求删除和放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49683960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com