HTTP Methods

HTTP Methods (HTTP方法)

HTTP (Hypertext Transfer Protocol) is created to provide communication between clients and the server. It works as a request and answer. (创建HTTP (超文本传输协议)以提供客户端和服务器之间的通信。它可以作为请求和回答。)

There are two basic HTTP methods: GET and POST.

GET Method

GET Method (获取方法)

The GET method of HTTP requests data from a specified source. GET requests can be cached and remain in the browser history. It can also be bookmarked. (HTTP的GET方法从指定的源请求数据。GET请求可以缓存并保留在浏览器历史记录中。也可以添加书签。)

It should never be used while working on sensitive data. GET requests have length restrictions, and they should be used only to get data. (切勿在处理敏感数据时使用。GET请求有长度限制,只能用于获取数据。)

The query strings (name/value pairs) are sent in the GET request’s URL. (>查询字符串(名称/值对)在GET请求的URL中发送。)

The code will look like this:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the document</title>
 </head>
 <body>
   <form action="/form/submit" method="get">
     First name:
     <input type="text" name="username" placeholder="Your name">
     <br/>
     <br/>
     <input type="submit" value="Submit">
   </form>
 </body>
</html>

POST Method

POST Method (我们将通过POST方法向您发送带有"AysQuiz"键的测验信息表中的所有数据。)

The POST method submits data to be processed to a specified source. As opposed to the GET method, POST requests are never cached and do not stay in the browser history, and we cannot bookmark them. Moreover, POST requests have no data length restrictions. (POST方法将要处理的数据提交到指定的源。与GET方法相反, POST请求从不缓存,也不会留在浏览器历史记录中,我们也不能将其添加为书签。此外, POST请求没有数据长度限制。)

The query strings (name/value pairs) are sent in the POST request’s HTTP Message body. (>查询字符串(名称/值对)在POST请求的HTTP消息正文中发送。)

The code will look like this:

<!DOCTYPE html>
<html>
 <head>
   <title>Title of the document</title>
 </head>
 <body>
   <form action="/form/submit" method="post">
     First name:
     <input type="text" name="username" placeholder="Your name">
     <br/><br/>
     <input type="submit" value="Submit">
   </form>
 </body>
</html>

Comparison of GET and POST Methods

Comparison of GET and POST Methods (GET和POST方法的比较)

GETPOST
Back button/ReloadHarmlessIt means that the data will be resubmitted. The browser must warn that the data will be re-submitted in this case.
Can be BookmarkedYesNo
Can be cachedYesNo
Encoding Typeapplication/x-www-form-urlencodedapplication/x-www-form-urlencoded or multipart/form-data
HistoryRemains in browser history.Doesn’t remain in browser history.
Data Length RestrictionsWhile sending data, GET method adds the data to the URL. The URL length is limited (maximum URL length is 2048 characters).Doesn’t have restrictions.
Data Type RestrictionOnly ASCII characters allowed.Doesn’t have restrictions. Binary data is also allowed.
SecurityIt is less secured than POST as the data sent is a part of the URL.POST is a little safer than GET as it is not remaining in browser history or web server logs.
VisibilityData is visible to everyone in the URL.Doesn’t show data in the URL.

Other HTTP Request Methods

Other HTTP Request Methods (其他HTTP请求方法)

Beside GET and POST methods, there are some other methods. See them below:

MethodDescription
HEADIt is the same with the GET method, but it returns only HTTP readers, not document body.
OPTIONSIt returns HTTP methods that are supported by the server.
CONNECTIt converts the request connection to a transparent TCP/IP tunnel.

Put Method

Put Method (Put方法)

The PUT method is mostly used for update abilities. In other words, with this method, we put a popular resource URL with the request body, which contains the representation of the original resource that has been recently updated. This method can also be used for generating a resource when the resource ID chooses the client, not the server. (PUT方法主要用于* 更新 *能力。换句话说,使用此方法,我们将流行的资源URL与请求正文放在一起,后者包含最近更新的原始资源的表示形式。当资源ID选择客户端而不是服务器时,此方法也可用于生成资源。)

Take into account that PUT isn’t considered as a safe method, because even if it creates or changes state on the server, it is idempotent. This means that if you create or modify the resource with this method and then make the same call for the second time, the resource will still be there with the same state as it did with the first one. (请注意, PUT不被视为安全方法,因为即使它在服务器上创建或更改状态,它也是幂等的。这意味着,如果使用此方法创建或修改资源,然后第二次进行相同的调用,则资源仍将具有与第一次相同的状态。)

The example below requests the server to save the given entity-body in method.py at the root of the server:

PUT /method.py HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.w3cdoc.com
Accept-Language: en-us
Connection: Keep-Alive
Content-type: text/html
Content-Length: 160

After storing the given entity-bode in the file, the server will send the following response:

HTTP/1.1 201 Created
Date: Tue, 13 Dec 2019 14:53:57 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 40
Connection: Closed

Patch Method

Patch Method (补丁方法)

The PATCH method is mostly used for modify abilities. It doesn’t need the whole resource. It only needs to contain the changes to the resource. This method is neither idempotent nor safe. Collisions between two PATCH requests can be very dangerous, as some patch formats need an operation from a popular base-point; otherwise, the resource will be corrupted. (PATCH方法主要用于* 修改 *能力。它不需要全部资源。它只需要包含对资源的更改。此方法既不幂等也不安全。两个PATCH请求之间的冲突可能非常危险,因为某些PATCH格式需要从流行的基点进行操作;否则,资源将被损坏。)

Delete Method

Delete Method (删除 Web 方法)

As you can guess, this method is used to delete a resource that is identified by a URL. This method is idempotent, too. In the case of deleting a resource, it will be removed. And if you call for DELETE for multiple times, the result will be the same - the resource will be removed. (您可以猜到,此方法用于* 删除 *由URL标识的资源。这种方法也是幂等的。在删除资源的情况下,它将被删除。如果您多次调用DELETE ,结果将是相同的-资源将被删除。)

The example below requests the server to delete the method.py file, at the root of the server:

DELETE /method.ry HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.w3cdoc.com
Accept-Language: en-us
Connection: Keep-Alive

After deleting the file, the server will send the following response:

HTTP/1.1 200 OK
Date: Tue, 13 Dec 2019 14:53:57 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 20
Connection: Closed


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部