Sub topic 8
GET versus POST
This chapter assumes you can build a basic form with an action attribute. It also assumes you know that each control with a name contributes a name and value pair to the submitted data.
What the method attribute controls
The method attribute decides where the browser puts the name and value pairs. If you leave it out, the form uses GET.
With GET, the browser appends the pairs to the action URL as a query string.
<form action="/search" method="get">
<label for="q">Search</label>
<input id="q" name="q" type="search">
<button type="submit">Search</button>
</form>If the user types html forms and submits, the browser requests /search?q=html+forms. The browser encodes spaces as + and escapes reserved characters.
With POST, the browser sends the same pairs in the request body, and the URL stays /subscribe.
<form action="/subscribe" method="post">
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>
<button type="submit">Subscribe</button>
</form>Encoding types
The enctype attribute sets how a POST body is encoded. It has no effect on GET.
| Value | Use |
|---|---|
| `application/x-www-form-urlencoded` | Default. Sends `name=value&name2=value2`. |
| `multipart/form-data` | Required for file inputs. A later chapter covers it. |
| `text/plain` | Meant for debugging. Avoid it in real forms. |
Choosing between them
Use GET when the request only reads data and can safely be repeated. Search, filtering, and sorting are typical cases. The result has its own URL, so users can bookmark it, share it, and use the back button.
Use POST when the request changes something on the server. Creating an account, sending a message, and placing an order are typical cases. Browsers warn before resubmitting a POST, and they never prefetch one.
Never use GET for passwords or other private values. URLs end up in browser history, server logs, and Referer headers. POST does not encrypt anything, though. Protect the data with HTTPS.
Common errors
- Forgetting
method="post"on a login form. The form submits with GET, and the password appears in the address bar. - Putting a query string in
actionof a GET form. The browser replaces that query string with the form's own pairs, so the original parameters are lost. Add them as hidden inputs instead. - Reloading a page that a POST produced. The browser asks to resubmit the form, which can create duplicate records. Have the server answer with a redirect to a GET page. This is the Post/Redirect/Get pattern.
- Expecting other verbs. HTML forms support only
GETandPOST. PUT and DELETE need JavaScript. - Sending large data by GET. URL length limits vary by browser and server, so long text belongs in a POST body.
Exercise
A product page has a form with a text field named sort and a select named category. Users want to bookmark filtered lists. Which method should the form use, and what URL results when sort is price and category is books, with action="/products"?
Show answer
Use GET. The form only reads data, and a GET result has a bookmarkable URL. The browser requests /products?sort=price&category=books.