HTTP Methods are messages to a server to indicate the kind of actions to take.
These methods allow for richer communication between the browser and the server.
The most commonly used methods are GET and POST.
HTTP methods: GET
, POST
, PUT
, DELETE
, and more.
HTTP stands for Hypertext Transfer Protocol (HTTP)
HTTP is an application protocol that enables a client and a server to communicate.
This protocol can be used to transmit messages and digital documents such as HTML.
A typical HTTP flow includes a client making a request to a server which then returns a response.
HTTP methods are HTTP requests that indicates what actions the server should perform.
These methods allow for a much richer communication between the client and the server.
These are the common HTTP methods.
GET
POST
PUT
PATCH
DELETE
HEAD
OPTIONS
Tip: Successful web development requires that developers have a good understanding of the commonly used HTTP methods, specifically GET and POST.
The GET method requests data from a server using URL parameters.
It is by far the most commonly used HTTP method on the web.
The GET request parameters are formatted as name-value pairs.
This example instructs Google to GET the results for recipes using the query parameter q
.
In response, Google will return a page with search results.
www.google.com/search?q=recipes
The POST method sends data to a server to change a resource (often a database record).
POST data is sent in the request body which cannot be seen by the user.
This is a an example POST request:
POST /customer/edit HTTP/1.1 Host: company.com id=1&firstname=Steve&lastname=Allison
GET and POST are the most commonly used methods.
This table summarizes their differences.
Question | GET | POST |
---|---|---|
Can request be bookmarked? | Yes | No |
Can request be cached? | Yes | No |
Does request stay in browser history? | Yes | No |
Is the data visible to the user? | Yes, parameters are visible in the URL. | No |
What happens when a browser request is refreshed? | Harmless | Data will be re-submitted and browser will prompt confirmation for re-submission. |
What are the encoding types? | application/x-www-form-urlencoded | application/x-www-form-urlencoded or multipart/form-data (for binary data) |
Any restrictions on data length? | Yes. The data is added to URL as name and value pairs. The max URL length is 2048 characters | No length restrictions |
Any restrictions on data type? | Only ASCII characters are allowed | No restrictions. |
How secure are these requests? | GET is less secure then POST since the data is part of the URL. GET should not be used for sending sensitive data (e.g. passwords, credit card information). | POST is safer than GET because the data is not visible to the user. Also, it is not stored in browser history or web server logs. |
Just like POST, the PUT method also sends data to a server for modification.
However, POST is used to add new data to a database, whereas PUT is used to update existing data.
Unlike POST, PUT always produces the same results, irrespective how often is being called.
PATCH is a more recently added HTTP method -- in 2010.
The difference between PATCH and PUT is subtle and can be confusing.
Generally, PUT is used to update entire records and all relevant data is included in the request body.
PATCH also updates data, but it only sends the data that need change -- much like a 'delta'.
The HEAD sends a request for data, but it does not receive any data from the source.
This method is used to validate a resource before calling it with a GET request.
The OPTIONS method defines the communication options for the target resource.