Sub topic 8
GET versus POST
This chapter assumes you can write a basic <form> with named controls and a submit button, and that you know how name and value pairs are built. You should also be comfortable reading a URL and opening your browser's Network panel. The form method decides where the browser puts those pairs and what the server may assume about the request.
The method attribute
The method attribute on <form> accepts get, post, and dialog. The default is get, so a form with no method sends a GET request. The value is case-insensitive. An invalid value falls back to get.
The browser builds the same list of name/value pairs for both methods. The methods differ in where that list goes:
| Row | GET | POST |
|---|---|---|
| Where the data goes | Query string of the URL | Request body |
| Visible in the address bar | Yes | No |
| Bookmarkable and shareable | Yes | No |
| Typical purpose | Reading, searching, filtering | Creating or changing something |
| Cached and prefetchable | Often | Not by default |
| Size limit | Practical URL length limit | Set by the server, usually large |
| File uploads | Not possible | Possible with `multipart/form-data` |
How a GET submission works
With GET, the browser takes the action URL, discards any query string already in it, and appends ? followed by the encoded pairs.
<form action="/search" method="get">
<label for="q">Search</label>
<input id="q" name="q" type="search">
<label for="sort">Sort by</label>
<select id="sort" name="sort">
<option value="new">Newest</option>
<option value="top">Top rated</option>
</select>
<button type="submit">Find</button>
</form>If the user types html forms and chooses "Newest", the browser requests this URL:
GET /search?q=html+forms&sort=new HTTP/1.1
Host: example.comThe encoding is application/x-www-form-urlencoded. Spaces become +, and reserved characters become percent escapes, so @ becomes %40. The request has no body.
This makes GET a good fit for search, filters, sorting, and pagination. The user can bookmark the result, copy the link, and use the back button without a "resubmit?" prompt.
Gotchas with GET
- A query string written into
action, such asaction="/search?lang=en", is dropped on submission. To keep a fixed value, add<input type="hidden" name="lang" value="en">. - Controls without a
nameare not sent. Unchecked checkboxes and unselected radio groups are not sent either. - URLs end up in browser history, server logs, proxy logs, and
Refererheaders sent to other sites. Never put passwords, tokens, or personal data in a GET form. - URLs have practical length limits. A figure around 2,000 characters is commonly cited, but servers and proxies differ. A large textarea in a GET form can fail unexpectedly.
- Crawlers, link prefetchers, and browser extensions may issue GET requests on their own. A GET request should never delete or change anything.
How a POST submission works
With POST, the URL stays clean and the pairs travel in the request body.
<form action="/subscribe" method="post">
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>
<label for="plan">Plan</label>
<select id="plan" name="plan">
<option value="free">Free</option>
<option value="pro">Pro</option>
</select>
<button type="submit">Subscribe</button>
</form>If the user enters [email protected] and chooses Pro, the browser sends this request:
POST /subscribe HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 32
email=ana%40example.com&plan=proThe encoding is the same as for GET. It sits in the body instead of the URL, and the Content-Type header tells the server how to parse it.
A POST body is not encrypted. Anyone who can read the traffic can read the body, unless the site uses HTTPS. POST keeps data out of the URL, history, and most logs. It does not make the data secret.
Safe, idempotent, and unsafe
HTTP defines properties that matter for form design:
- A safe method does not change server state. GET is safe.
- An idempotent method has the same effect whether you send it once or ten times. GET is idempotent, and so are PUT and DELETE.
- POST is neither. Sending it twice may create two orders.
Browsers rely on these definitions. When a user reloads a page that came from a POST, the browser warns that the form will be resubmitted. For a GET, it reloads silently. Caches may store GET responses and will not reuse POST responses unless explicitly told to.
Choosing between them
Ask these questions in order:
- Does the submission change data, send a file, or carry something private? Use POST.
- Is it a read-only query that the user might want to bookmark or share? Use GET.
- Could the data be large, such as a long textarea? Use POST.
- Is the result a different view of the same resource, such as a filtered list? Use GET.
The enctype attribute
For POST, the enctype attribute chooses how the body is encoded. It also exists on the submit button as formenctype.
| Value | Use |
|---|---|
| `application/x-www-form-urlencoded` | Default. Compact text pairs. |
| `multipart/form-data` | Required when the form has a file input. Each field is a separate part. |
| `text/plain` | Human-readable, but ambiguous to parse. Avoid it in production. |
A form with a file input needs both settings:
<form action="/avatar" method="post" enctype="multipart/form-data">
<label for="photo">Photo</label>
<input id="photo" name="photo" type="file" accept="image/*">
<button type="submit">Upload</button>
</form>If you leave out enctype or use method="get", the browser sends only the file name, not the file contents.
Overriding the method per button
A submit button can override the form's settings with formmethod, formaction, and formenctype. This lets one form offer two actions.
<form action="/posts/42" method="post">
<label for="body">Comment</label>
<textarea id="body" name="body"></textarea>
<button type="submit">Post comment</button>
<button type="submit" formmethod="get" formaction="/preview">Preview</button>
</form>The second button sends the body text as a GET request to /preview. For a long comment, that URL can hit the length limit. A POST preview endpoint would be safer here.
Post/Redirect/Get
A server that answers a POST with a normal page leaves the browser in a bad state. A refresh resends the POST, and the back button shows a resubmit warning. The standard fix is Post/Redirect/Get. The server processes the POST, then replies with a redirect, and the browser follows it with a GET.
Since the instructions require a specific fence form, here is the diagram in that form:
Use status 303 so the browser always switches to GET. With 302, historical behavior varies between clients. After the redirect, reloading /thanks repeats only the harmless GET.
Other values: dialog
method="dialog" closes the <dialog> element that contains the form. It sends nothing to a server. The clicked button's value becomes the dialog's returnValue. It is useful for confirmation popups, but it is not a network method.
Common errors
- Using GET for a login form. The password lands in the URL and in logs.
- Forgetting
method="post". The form silently defaults to GET, and data appears in the address bar. - Expecting POST to be secure. Without HTTPS, the body is readable on the network.
- Uploading files without
enctype="multipart/form-data". The server receives file names but no content. - Deleting data through a GET link or form. A prefetcher can trigger it.
- Not redirecting after a POST. Users see resubmit prompts and may create duplicate records.
Exercise
A page has a form with action="/results?page=2" and method="get" containing one input named q. The user submits the value cats. What URL does the browser request, and how would you keep page=2 in the request?
Show answer
The browser requests /results?q=cats. A GET submission replaces the query string in action, so page=2 is lost. To keep it, add <input type="hidden" name="page" value="2"> inside the form. The request then becomes /results?q=cats&page=2.
Exercise
You are building a form that lets users upload a profile photo and change their display name. Which method and enctype do you use, and what should the server respond with after saving?
Show answer
Use method="post" with enctype="multipart/form-data", because a file input needs both. After saving, the server should answer with a 303 redirect to a page such as the profile view, following Post/Redirect/Get, so a refresh does not resend the upload.