GET Requests
GET requests are primarily used to retrieve data from a server. When a client sends a GET request, it asks the server to provide a representation of a specific resource.
GET requests are considered:
- Safe — they should not modify data
- Idempotent — repeating the request produces the same result
Meaning it should not modify any data on the server and can be repeated without side effects.
<form action="/search" method="GET"> <input type="text" name="q"> <button type="submit">Search</button> </form>
Data in a GET request is appended to the URL as query parameters. For example:
GET /api/users?id=123&name=John
How GET requests work
- Data is appended to the URL as query parameters
- Parameters are visible in the browser’s address bar
- The URL can be bookmarked or shared
POST Requests
POST requests are used to send data to the server to create or modify resources. When a client sends a POST request, it includes the data in the request body. This method is considered "non-idempotent," meaning the same request repeated multiple times can result in different outcomes.
Data in a POST request is included in the request body. For example:
POST /api/users
Content-Type: application/json
{
"id": 123,
"name": "John"
}
POST data isn’t shown in the URL, which helps avoid accidental exposure — but real security comes from using HTTPS.
Why POST is preferred for forms
- Data is not visible in the URL
- More secure for sensitive input
- Allows sending larger amounts of data
Differences and Use Cases
Key Differences Between GET and POST
| Feature | GET | POST |
|---|---|---|
| Purpose | Retrieve data | Submit or modify data |
| Data location | URL (query string) | Request body |
| Visibility | Visible in address bar | Not visible |
| Security | Low | Higher |
| Bookmarkable | Yes | No |
| Data size | Limited | Larger payloads |
When to Use GET Requests
The main differences between POST and GET requests can be summarized as follows:
- GET requests are used for data retrieval, while POST requests are used for data submission or modification.
- GET requests append data to the URL as query parameters, while POST requests include data in the request body.
- GET requests are visible in the browser's address bar, while POST requests are not.
Use GET requests when:
- Retrieving data from the server.
- Accessing public information that does not require sensitive data.
- Sending lightweight data or parameters.
Examples:
- Search pages
- Pagination
- Filtering lists
GET requests are ideal when the request does not change server data.
When to Use POST Requests
Use POST requests when:
- Submitting form data or user input.
- Modifying data on the server.
- Sending sensitive data that should not be visible in the URL or browser history.
Examples:
- Login forms
- Registration forms
- Contact forms
- File uploads
POST requests are the correct choice whenever user input is involved.
Understanding the differences between POST and GET requests allows you to choose the appropriate method based on the purpose and requirements of your application.
Remember to follow best practices and security considerations when handling user data, especially with POST requests. In the next lecture, we'll explore HTML media elements and how to incorporate images, audio, and video into your webpages.
GET and POST in HTML Forms (Connection to Previous Lesson)
In HTML forms, the request method is defined using the method attribute of the <form> element.
<form method="GET" action="/search">...</form> <form method="POST" action="/search">...</form>
Choosing the correct method:
- Improves security
- Prevents accidental data exposure
- Ensures proper server behavior
This makes GET and POST a critical concept when working with forms.
Conclusion
GET and POST requests are fundamental to how the web works. Understanding their differences allows you to:
- Build secure forms
- Communicate correctly with servers
- Avoid common data-handling mistakes
As you continue learning HTML, these concepts will reappear in form handling, backend communication, and later when working with JavaScript and APIs.