Sobes.tech
Junior — Middle

Can you explain how the HTTP GET method works for retrieving data?

sobes.tech AI

Answer from AI

The HTTP GET method is used to request data from a server. When a client (such as a browser) sends a GET request, it asks the server to return a specific resource or information. Parameters can be passed in the URL after the question mark.

Features of GET:

  • Data is transmitted in the URL, so it is limited in length.
  • GET requests should be idempotent — repeated requests should not change the server's state.
  • Usually used for retrieving data, not modifying it.

Example of a GET request in PHP:

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $id = $_GET['id'] ?? null; // getting the id parameter from the URL
    if ($id) {
        // logic to retrieve data by id
        echo "Data for id: " . htmlspecialchars($id);
    } else {
        echo "The id parameter is not specified.";
    }
}