简体   繁体   English

http请求删除并放入

[英]http request delete and put

Below is my code for HTTP request for getting and post. 下面是我的获取和发布HTTP请求的代码。 What I wanted to know is how to do HTTP request for delete and put. 我想知道的是如何进行删除和放置的HTTP请求。 I have made it possible for get and post to work. 我已经让get和post工作成为可能。 I want to know how on the part of delete and put based on my code below. 我想知道如何根据我的代码删除和放置。 what to change in my postcode when I want to change it to delete? 当我想要将其更改为删除时,我的邮政编码中要更改什么? what is lacking? 缺少什么? I wanted to delete using ID like for example "id": 16, 我想删除使用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()

get 得到

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()

The only one that differs is GET with the parameters are in the url itself , POST & DELETE & PUT are work the same way , just change the httpMethod parameter and specify httpBody if you wanted to , meaning 唯一不同的是GET ,参数在url本身, POST&DELETE&PUT的工作方式相同,只需更改httpMethod参数并指定httpBody即可,意思是

DELETE : means delete a resource from a specific url DELETE:表示从特定URL删除资源

PUT : place a resource in to a web server PUT:将资源放入Web服务器

// //

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  

You should go through difference between each method types. 您应该了解每种方法类型之间的差异。 It will help you, what should you do and when. 它会帮助你,你应该做什么,什么时候做。


PUT

Store an entity at a URI. 将实体存储在URI中。 PUT can create a new entity or update an existing one. PUT可以创建新实体或更新现有实体。 A PUT request is idempotent. PUT请求是幂等的。 Idempotency is the main difference between the expectations of PUT versus a POST request. 幂等性是PUT与POST请求的期望之间的主要区别。

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. PUT方法请求将所包含的实体存储在提供的Request-URI下。 If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. 如果Request-URI引用已经存在的资源,则封闭的实体应该被视为驻留在源服务器上的实体的修改版本。 If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI. 如果Request-URI未指向现有资源,并且该URI能够被请求用户代理定义为新资源,则源服务器可以使用该URI创建资源。 If a new resource is created, the origin server MUST inform the user agent via the 201 (Created) response. 如果创建了新资源,则源服务器必须通过201(已创建)响应通知用户代理。 If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request. 如果修改了现有资源,则应该发送200(OK)或204(No Content)响应代码以指示请求的成功完成。 If the resource could not be created or modified with the Request-URI, an appropriate error response SHOULD be given that reflects the nature of the problem. 如果无法使用Request-URI创建或修改资源,则应该给出适当的错误响应,以反映问题的性质。 The recipient of the entity MUST NOT ignore any Content-* (eg Content-Range) headers that it does not understand or implement and MUST return a 501 (Not Implemented) response in such cases. 实体的接收者绝不能忽略它不理解或实现的任何Content- *(例如Content-Range)头,并且在这种情况下必须返回501(未实现)响应。

Modify the address with an ID of 1: 修改ID为1的地址:

PUT /addresses/1 PUT /地址/ 1
Note: PUT replaces an existing entity. 注意:PUT替换现有实体。 If only a subset of data elements are provided, the rest will be replaced with empty or null. 如果仅提供数据元素的子集,则其余数据元素将替换为空或null。

urlRequestInstance.httpMethod = "PUT"

DELETE 删除

Request that a resource be removed; 请求删除资源; however, the resource does not have to be removed immediately. 但是,不必立即删除资源。 It could be an asynchronous or long-running request. 它可能是异步或长时间运行的请求。

The DELETE method requests that the origin server delete the resource identified by the Request-URI. DELETE方法请求源服务器删除Request-URI标识的资源。 This method MAY be overridden by human intervention (or other means) on the origin server. 可以通过源服务器上的人为干预(或其他方式)覆盖此方法。 The client cannot be guaranteed that the operation has been carried out, even if the status code returned from the origin server indicates that the action has been completed successfully. 即使从源服务器返回的状态代码指示操作已成功完成,也无法保证客户端已执行该操作。 However, the server SHOULD NOT indicate success unless, at the time the response is given, it intends to delete the resource or move it to an inaccessible location. 但是,服务器不应该指示成功,除非在给出响应时,它打算删除资源或将其移动到不可访问的位置。

A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity. 如果响应包括描述状态的实体,则成功响应应为200(OK),如果操作尚未执行,则应为202(已接受);如果操作已颁布但响应不包括,则应为204(无内容)一个实体。

Delete an address with an ID of 1: 删除ID为1的地址:

DELETE /addresses/1 DELETE / addresses / 1

urlRequestInstance.httpMethod = "DELETE"

Here are nice tutorial references for you: 这里有很好的教程参考资料:

Now you can try a sample code, answered by Sh_Khan 现在您可以尝试一个示例代码,由Sh_Khan回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM