In HTML, the method="post"
attribute used within a <form>
tag specifies how form data should be sent to the server. It's a crucial part of handling user input on websites.
The method="post"
attribute value specifies that the form data will be sent to the server by storing it in an HTTP request body. This method is used to transfer data securely using HTTP headers.
Understanding the POST Method
When you submit an HTML form with method="post"
, the data entered by the user is packaged and included in the body of the HTTP request sent to the server. Unlike the GET
method, which appends data to the URL, POST
keeps the data separate from the URL visible in the browser's address bar.
Key Characteristics:
- Data Location: Data is sent in the HTTP request body.
- URL: Form data is not appended to the URL.
- Visibility: Less visible than
GET
as data isn't in the URL. - Security (relative): While not encrypted by default (unless using HTTPS), sending data in the body makes it less susceptible to being accidentally bookmarked, logged in browser history, or easily seen by someone looking over your shoulder. This is why it's often used for sensitive information.
- Data Size: Generally has no strict limit on the amount of data that can be sent (unlike
GET
which has URL length limits). - Server Impact: Typically used for requests that change the state of the server, such as creating a new resource, updating data, or sending an email.
- Idempotency:
POST
requests are generally not considered idempotent. Submitting the samePOST
request multiple times can result in creating multiple resources or performing the same action multiple times.
Why Use POST?
You would typically choose the POST
method for forms when:
- You are sending sensitive data like passwords, credit card information, or personal details.
- You are uploading files.
- You are submitting large amounts of data that might exceed URL length limits.
- The form submission results in a change on the server (e.g., creating a new user account, submitting an order, posting a comment).
How POST Data is Transferred
As the reference states, data transferred via POST
is stored in the HTTP request body and sent securely using HTTP headers. While standard HTTP is not encrypted, the way POST
uses headers and the body makes it a more secure way to transfer certain types of data compared to GET
, especially when combined with HTTPS.
Consider a login form:
<form action="/login" method="post">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
When this form is submitted, the username and password will be sent in the body of the HTTP POST request to /login
on the server. This prevents the sensitive information from appearing in the browser's address bar or history.
POST vs. GET
Here's a quick comparison between the two primary methods for sending form data:
Feature | GET Method | POST Method |
---|---|---|
Data Location | Appended to the URL (Query String) | In the HTTP Request Body |
Visibility | Data visible in URL | Data not visible in URL |
Data Size | Limited by URL length | No strict limit (determined by server) |
Bookmarks | Can be bookmarked | Cannot be easily bookmarked |
Browser Hist. | Data stored in browser history log | Data not stored in browser history log with URL |
Use Case | Retrieving data, searching | Submitting data, uploading files, sensitive data |
Idempotency | Idempotent (usually) | Not Idempotent |
Conclusion
In summary, the method="post"
attribute in HTML forms directs the browser to send form data in the body of an HTTP request. This is the preferred method for sensitive data, large datasets, and operations that modify server state, providing a more secure and robust way to handle form submissions compared to sending data via the URL.