Intern
What types of HTTP requests are there?
sobes.tech AI
Answer from AI
Main types of HTTP requests: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE, CONNECT.
Description of the most common:
- GET: Requests a representation of the specified resource. Requests using this method should only retrieve data.
- POST: Sends data to be processed to a specific resource. A common result is a change in state or side effects on the server.
- PUT: Replaces all current representations of the target resource with the data from the request body.
- DELETE: Deletes the specified resource.
- PATCH: Applies partial modifications to a resource.
- HEAD: Requests headers similar to
GET, but without the response body. Used for obtaining metadata. - OPTIONS: Describes communication parameters for the target resource.
- CONNECT: Converts the request to a transparent TCP/IP proxy connection.
- TRACE: Performs a message loop-back test along the path to the target resource.
Differences between GET and POST:
| Attribute | GET | POST |
|---|---|---|
| Purpose | Retrieve data | Send data for processing |
| Data transmission | Via URL parameters (Query String) | Via request body |
| Security | Idempotent (repeated requests do not change state) | Not idempotent |
| Data visibility | Visible in URL, stored in browser history, server logs | Not visible in URL, not stored in browser history |
| Data size | Limited by URL length | Unlimited (or very large) |
| Caching | Can be cached | Not cached by default |
| Bookmarks | Can add a page with GET parameters to bookmarks | Cannot (for regular form submission methods) |
Example of using GET in PHP:
<?php
// Getting the 'id' parameter from URL
$userId = $_GET['id'];
echo "User ID: " . htmlspecialchars($userId);
?>
Example of using POST in PHP (form data processing):
<?php
// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get data from request body
$username = $_POST['username'];
$password = $_POST['password'];
// Further processing of data (e.g., saving to database)
echo "Username: " . htmlspecialchars($username) . "<br>";
echo "Password received.";
}
?>