Go to: http get post put Delete

Source: Internet
Author: User
Tags representational state transfer
Essential differences between http post get
I. Differences of principles

Generally, you can enter a URL in your browser to access resources by using the get method. In form submission, you can specify the submission method as get or post. The default method is get submission.

HTTP defines different methods for interaction with the server. There are four basic methods: Get, post, put, and delete.

The full name of a URL is a resource descriptor. We can think that a URL address is used to describe resources on a network, while get, post, put, delete corresponds to the query, modify, add, and delete operations on this resource. Here, we should have a rough understanding. Get is generally used to obtain/query resource information, while post is generally used to update resource information (I personally think this is the essential difference between get and post, it is also the intent of the Protocol designer. Other differences are the differences in specific forms ).

According to the HTTP specification, get is used to obtain information, and should be secure and idempotent.

1. The so-called security means that this operation is used to obtain information rather than modify information. In other words, get requests generally do not have side effects. That is to say, it only obtains the resource information, just like the database query. It does not modify, add data, and does not affect the resource status.

* Note: security only indicates that the information is not modified.

2. idempotence means that multiple requests to the same URL should return the same result. Here I will explain the concept of idempotence:

Idempotence (idempotent and idempotence) is a mathematical or computer concept that is common in abstract algebra.
Idempotence has the following definitions:
For a single-object operation, if an operation is performed multiple times for all the numbers in the range, the result is the same as that obtained once, this operation is called idempotent. For example, an absolute value operation is an example. In a real number set, ABS (A) = ABS (a) is used )).
For binary operations, it is required that when the two values involved in the calculation are equivalent, if the calculation result is equal to the two values involved in the calculation, the operation is called the idempotence, for example, a function that calculates the maximum values of two numbers has the power in the real number set, that is, Max (x, x) = x.
After reading the above explanation, you should be able to understand the meaning of the get power.

However, in practice, the above two rules are not so strict. Example of referencing others' articles: for example, the front pages of news sites are constantly updated. Although the second request will return a different batch of news, this operation is still considered safe and idempotent because it always returns the current news. Basically, if the target is to open a link, the user can be sure that the resource is not changed from his own perspective.

According to the HTTP specification, post indicates a request that may modify resources on the server. Continue to reference the above example: for news websites, readers should post their comments on news, because the Site Resources are different after the comments are submitted, or the resource is modified.

The above describes some of the principles of get and post in the HTTP specification. However, in practice, many people fail to follow the HTTP specification, which leads to many reasons, such:

1. Many users are greedy and convenient. Get is used to update resources, because form is required for post, which may cause a little trouble.

2. You can add, delete, modify, and query resources through get/post without using put and delete.

3. In the early stage, Web MVC Framework designers did not consciously view and design URLs as abstract resources. Another serious problem is that the traditional Web MVC framework basically only supports the get and post HTTP methods, rather than the put and delete methods.

* MVC: MVC originally exists in the desktop program, M is the exponential data model, V is the user interface, and C is the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different expressions.

The above three points are a typical description of the old style (not strictly compliant with HTTP specifications). With the development of the architecture, there is now a representational state transfer and a new style that supports HTTP specifications, for more information, see restful Web Services.

2. Differences in Forms

After clarifying the differences between the two principles, let's take a look at the differences in their actual application:

To understand the differences between the two during transmission, Let's first look at the HTTP protocol format:

HTTP request:


]

In an HTTP request, the first line must be a request line to describe the request type, resources to be accessed, and the HTTP Version Used. Next is a header section, which describes additional information to be used by the server. After the header is a blank line, you can add any other data [called the body].

Example of get and post methods:
GET/books /? Sex = Man & name = Professional HTTP/1.1
HOST: www.wrox.com
User-Agent: Mozilla/5.0 (windows; U; Windows NT 5.1; en-US; RV: 1.7.6)
Gecko/20050225 Firefox/1.0.1
Connection: keep-alive

Post, HTTP, 1.1
HOST: www.wrox.com
User-Agent: Mozilla/5.0 (windows; U; Windows NT 5.1; en-US; RV: 1.7.6)
Gecko/20050225 Firefox/1.0.1
Content-Type: Application/X-WWW-form-urlencoded
Content-Length: 40
Connection: keep-alive
(---- Null Line here ----)
Name = Professional % 20 Ajax & publisher = Wiley

With the above understanding and examples of HTTP requests, let's look at the differences between the two submission methods:

(1) When a GET request is submitted, the request data is appended to the URL (that is, the data is placed in the HTTP header? Splits the URL and transmits data. Multiple parameters are connected with &. For example, login. Action? Name = hyddd & Password = idontknow & verify = % E4 % BD % A0 % E5 % a5 % BD. If the data is an English letter/number, it is sent as is. If it is a space, it is converted to +. If it is a Chinese character/other character, it is directly encrypted with base64, and the result is as follows: % E4 % BD % A0 % E5 % a5 % BD, where xx in % XX represents the ASCII represented in hexadecimal notation.

Post submission: place the submitted data in the packet body of the http package. In the preceding example, the red font indicates the actual data transmission.

Therefore, the data submitted by get will be displayed in the address bar, while the address bar will not change when the post is submitted.

(2) Size of transmitted data: first, it is declared that the HTTP protocol does not limit the size of transmitted data, and the HTTP protocol does not limit the URL length.

In actual development, the following restrictions exist:

Get: the URL length of a specific browser or server is limited. For example, the URL Length of IE is limited to 2083 bytes (2 k + 35 ). For other browsers, such as Netscape and Firefox, there is no length limit theoretically. The limit depends on the support of the operating system.

Therefore, when a GET request is submitted, data transmission is limited by the URL length.

Post: theoretically, data is not limited because the value is not transmitted through a URL. However, the actual size of the data submitted by post is limited on each Web server. Apache and IIS6 have their own configurations.

(3) Security:

. Post is more secure than get. Note: The security mentioned here is not the same as the "Security" mentioned in get. The above "security" only means not to modify data, but here security means the meaning of true security. For example, if you submit data through get, the user name and password will appear in the URL in plain text, because (1) the login page may be cached by the browser, (2) if others view the browser's historical records, they will be able to get your account and password. In addition, using get to submit data may also cause cross-site request forgery attacks.

(4) http get, post, and soap protocols all run on HTTP.
1) Get: The request parameter is appended to the URL as a sequence (query string) of a key/value pair.
The length of the query string is limited by the web browser and the Web server (for example, ie supports a maximum of 2048 characters). It is not suitable for transmitting large datasets and is not safe.
2) post: Request Parameters are transmitted in a different part of the HTTP header (Named Entity body). This part is used to transmit form information. Therefore, you must set Content-Type: application/X-WWW-form-urlencoded. Post is designed to support user fields on web forms. Its parameters are also transmitted as key/value pairs.
However, it does not support complex data types, because post does not define the semantics and rules of the transmitted data structure.
3) soap: a special version of http post, which follows a special XML Message format.
Content-Type is set to text/XML. Any data can be XML-based.

3. Http response
1. Http response format:



[ ]

The only real difference in the response is that the request information is replaced by the State Information in the first line. Status line describes the requested resources by providing a status code.

HTTP Response instance:

HTTP/1.1 200 OK
Date: sat, 31 Dec 2005 23:59:59 GMT
Content-Type: text/html; charset = ISO-8859-1
Content-Length: 122
<HTML>
<Head>
<Title> wrox homepage </title>
</Head>
<Body>
<! -- Body goes here -->
</Body>
</Html>
2. The most common status codes are:

◆ 200 (OK): The resource is found and everything is normal.
◆ 304 (not modified): The resource has not been modified since the last request. This is usually used for browser caching.
◆ 401 (unauthorized): the client has no permission to access this resource. This usually requires the user to enter the user name and password in the browser to log on to the server.
◆ 403 (Forbidden): the client is not authorized. This is usually because an incorrect user name or password is entered after 401.
◆ 404 (not found): the requested resource does not exist at the specified position.

Example 4:

Example:

HTTP GET

Send

GET/demowebservices2.8/service. asmx/cancelorder? Userid = string & Pwd = string & orderconfirmation = string HTTP/1.1
HOST: api.efxnow.com

Reply

HTTP/1.1 200 OK
Content-Type: text/XML; charset = UTF-8
Content-Length: Length

Boolean String Int Long String String

HTTP POST

Send

Post/demowebservices2.8/service. asmx/cancelorder HTTP/1.1
HOST: api.efxnow.com
Content-Type: Application/X-WWW-form-urlencoded
Content-Length: Length

Userid = string & Pwd = string & orderconfirmation = string

Reply

HTTP/1.1 200 OK
Content-Type: text/XML; charset = UTF-8
Content-Length: Length

Boolean String Int Long String String

HTTP 1.2

Send

Post/demowebservices2.8/service. asmx HTTP/1.1
HOST: api.efxnow.com
Content-Type: Application/soap + XML; charset = UTF-8
Content-Length: Length

String String String

Reply

HTTP/1.1 200 OK
Content-Type: Application/soap + XML; charset = UTF-8
Content-Length: Length

Boolean String Int Long String String

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.