HTTP Methods: GET vs POST
Level 4 · HTTP, the language of the web
What GET, POST, PUT, PATCH and DELETE each ask a server to do. Where the data goes, in a query parameter or in the request body.
Course contentsLesson 10 of 13
Level 1 · Networks and packets
Level 2 · Addresses and ports
Level 3 · How data travels
Level 4 · HTTP, the language of the web
Level 5 · Locked lines and go-betweens
After this lesson you can
- Pick the right method for a job
- Tell PUT from PATCH
- Say when data goes in the query or the body
12 mincovers 8 glossary entries
Worth reading first: HTTP Requests and Responses
What each HTTP method asks for
The method is the first word of an HTTP request, and it says what to do. There are five common ones. GET reads. POST adds something new. PUT replaces a thing whole. PATCH changes part of it. DELETE removes it. The same address can take all five and do something different each time.
GET only reads and changes nothing. That makes it safe to repeat, to save as a bookmark and to keep a copy of. Opening a page, loading an image and searching a shop are all GET requests.
POST sends new data to be added. Placing an order, posting a comment and signing up are POST requests. Sending the same POST twice can make two orders. That is why a browser warns you before it sends a form again.
PUT and PATCH both change a thing that exists. PUT sends the whole new version and replaces the old one. PATCH sends only the fields that change. Sending a PUT with one field missing can wipe that field out.
Data rides in one of two places. A query parameter sits in the address after a question mark, such as ?page=2. It suits short facts for reading, such as a search word or a page number. The request body sits after the headers and suits larger data or anything private, such as a form.
Addresses get saved in browser history, in server logs and in links people share. So anything after the question mark should be safe for others to see. A password or a card number always goes in the body of a POST. It never goes in the address.
A common mistake is to use GET for something that changes data, such as a link that deletes an item. Browsers and tools fetch GET links ahead of time to make pages faster. One of them can follow that link. The item is gone with no one clicking anything.
The five common methods
Which one to use
Worked example
A shop's search and checkout
A customer searches the shop for blue mugs. The browser sends GET /search?q=blue+mugs. The search word rides in the address, so the result page can be bookmarked.
At checkout, the browser sends POST /orders. The address, card details and two mugs go in the request body, where they never show up in history.
The words for this lesson8
Check yourself
Where should a password go in an HTTP request?
Finished this lesson?
Marking it complete also marks its 8 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.