← Back to HTML & CSS, page 10

html-css-0185 · Easy fresher

What is the difference between GET and POST in a form, and what do action and enctype do?

GET puts the data in the address and suits searches that change nothing. POST sends the data in the body and suits changes, private data and files. action is the target address, and enctype must be multipart/form-data for file uploads. Only named, enabled fields are sent.

Full answer

A form sends its fields to a server. Three attributes control how.

  • action is the address that receives the data. If it is missing, the form sends to the current page address.
  • method is get or post. If it is missing, the default is get.
  • enctype is the format of the body for post. The default is application/x-www-form-urlencoded. Use multipart/form-data when the form has a file input. text/plain exists, but it is for testing.

GET puts the data in the address, as in /search?q=hello+world. Use it for reading and searching, where the request does not change anything on the server. The address can be bookmarked, shared and cached, and the back button works well. Do not use it for passwords or private data, because addresses are stored in the history, in server logs and in referrer headers. Addresses also have a length limit.

POST puts the data in the body of the request. Use it when the request creates or changes something, such as signing up, paying or posting a comment, and for private data and for files. A POST request is not stored in the address bar and not bookmarked. After a POST, the server should answer with a redirect to a new page (the post/redirect/get pattern), so that reloading the page does not send the form a second time.

Neither method is safe on its own. Private data needs HTTPS in both cases, and POST forms that change data need protection against cross-site request forgery, for example a token. Also, validate on the server, because the browser checks can be skipped.

Only fields with a name are sent. Unchecked checkboxes and disabled fields are not sent.

The first form creates the address /search?q=hello+world. The second form sends the file in a multipart body. The third form has no method, so it uses get, and the default encoding is the URL-encoded format.

Choose the method by what the request does, and not by how the address looks.

html
<form id="f1" action="/search" method="get">
  <label for="q">Search</label>
  <input id="q" name="q" value="hello world">
  <button type="submit">Go</button>
</form>
<form id="f2" action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="photo">
  <button type="submit">Upload</button>
</form>
<form id="f3" action="/comment">
  <input name="text">
</form>

The check read the form properties and the data that the form would send. No server received a request. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • action is the target; the default method is get.
  • GET data is in the address; use it for searching.
  • POST data is in the body; use it for changes and files.
  • Files need enctype="multipart/form-data".
  • Only fields with a name are sent.

Common mistakes

  • Sending passwords with GET.
  • Forgetting enctype="multipart/form-data" on a form with a file input.
  • Forgetting the name attribute, so the field is not sent.

Follow-ups

  • What is the default method of a form?
  • What is the post/redirect/get pattern?

Tips

  • Link the method to the meaning: GET reads, POST changes.
  • Mention that hiding data in POST is not security by itself.