HTML request methods

What is HTTP?

Hypertext Transfer Protocol (HTTP, abbreviated as HTTP) is designed to enable communication between clients and servers.

HTTP acts as a request-response protocol between the client and server.

For example, the client (browser) sends an HTTP request to the server; then the server responds back to the client. The response contains status information about the request, and may also contain the requested content.

HTTP method

  • GET
  • POST
  • PUT
  • HEAD
  • DELETE
  • PATCH
  • OPTIONS

The most commonly used methods are: GET and POST.

GET method

GET is used to request data from a specified resource.

GET is one of the most common HTTP methods.

Please note that the query string (name/value pair) is sent in the URL of the GET request:

/test/demo_form.php?name1=value1&name2=value2

Some other comments about GET requests:

  • GET requests can be cached
  • GET requests are retained in the browser history
  • GET requests can be saved as bookmarks
  • GET requests should not be used when handling sensitive data
  • GET requests have length limits
  • GET requests should only be used to retrieve data (not to modify)

POST method

POST is used to send data to the server to create/update a resource.

The data sent to the server through POST is stored in the request body of the HTTP request:

POST /test/demo_form.php HTTP/1.1
Host: codew3c.com
name1=value1&name2=value2

POST is one of the most common HTTP methods.

Some other comments about POST requests:

  • POST requests will not be cached
  • POST requests will not be retained in the browser history
  • POST cannot be saved as a bookmark
  • There is no requirement for data length in POST requests

PUT method

PUT is used to send data to the server to create/update a resource.

The difference between POST and PUT lies in the fact that PUT requests are idempotent (idempotent). That is, multiple calls to the same PUT request will always produce the same result. Conversely, repeating the POST request has the side effect of creating the same resource multiple times.

HEAD method

HEAD is almost the same as GET, but without the response body.

In other words, if GET /users returns a list of users, then HEAD /users will send the same request but will not return the user list.

The HEAD request is very useful for checking the content that will be returned by the GET request before actually sending it (for example, before downloading a large file or before the response body).

DELETE method

The DELETE method deletes the specified resource.

OPTIONS method

The OPTIONS method describes the communication options of the target resource.

Comparison of GET and POST

The following table compares two HTTP methods: GET and POST.

  GET POST
Back button/refresh Harmless The data will be resubmitted (the browser should inform the user that the data will be resubmitted).
Bookmark Can be saved as a bookmark Cannot be saved as a bookmark
Cache Can be cached Cannot be cached
Encoding Type application/x-www-form-urlencoded application/x-www-form-urlencoded or multipart/form-data. Use multiple encoding for binary data.
History Parameters are retained in the browser history. Parameters are not stored in the browser history.
Restrictions on data length Yes. When sending data, the GET method adds data to the URL; the length of the URL is limited (the maximum length of the URL is 2048 characters). Unrestricted.
Restrictions on data types Only allows ASCII characters. No restrictions. Also allows binary data.
Security

Compared to POST, GET has poorer security because the data sent is part of the URL.

Never use GET when sending passwords or other sensitive information!

POST is safer than GET because parameters are not stored in the browser history or web server logs.
Visibility Data is visible to everyone in the URL. Data will not be displayed in the URL.