HTTP Methods

Understanding different ways to send HTTP requests

🔧 What are HTTP Methods?

HTTP methods (also called verbs) indicate the desired action to be performed on a resource. They tell the server what you want to do with the data.


<!-- GET method - retrieve data -->
<a href="page.html">Get Page</a>

<!-- POST method - send data -->
<form method="POST" action="submit.php">
    <input type="text" name="username">
    <input type="submit" value="Submit">
</form>
                                    

Common HTTP Methods

📥

GET

Retrieve data from server

<a href="page.html">Link</a>
<form method="GET">...</form>
📤

POST

Send data to server

<form method="POST">
    <input type="text">
    <input type="submit">
</form>
✏️

PUT

Update/replace resource

// JavaScript example
fetch('/api/user/123', {
    method: 'PUT',
    body: JSON.stringify(data)
})
🗑️

DELETE

Remove resource

// JavaScript example
fetch('/api/user/123', {
    method: 'DELETE'
})

🔹 GET Method

GET is used to retrieve data from the server. It's the default method for links and forms without a method attribute.

<!-- Links use GET by default -->
<a href="products.html">View Products</a>
<a href="search.php?q=html">Search for HTML</a>

<!-- Forms can use GET -->
<form method="GET" action="search.php">
    <input type="text" name="q" placeholder="Search...">
    <input type="submit" value="Search">
</form>

<!-- Results in: search.php?q=user+input -->

GET Characteristics:

  • Safe: Doesn't change server data
  • Idempotent: Same result every time
  • Cacheable: Browsers can cache responses
  • URL Parameters: Data visible in URL

🔹 POST Method

POST is used to send data to the server, typically for creating or updating resources.

<!-- Contact form using POST -->
<form method="POST" action="contact.php">
    <input type="text" name="name" placeholder="Your Name">
    <input type="email" name="email" placeholder="Your Email">
    <textarea name="message" placeholder="Your Message"></textarea>
    <input type="submit" value="Send Message">
</form>

<!-- Login form using POST -->
<form method="POST" action="login.php">
    <input type="text" name="username" placeholder="Username">
    <input type="password" name="password" placeholder="Password">
    <input type="submit" value="Login">
</form>

POST Characteristics:

  • Not Safe: Can change server data
  • Not Idempotent: May have different results
  • Not Cacheable: Responses not cached by default
  • Request Body: Data sent in request body, not URL

🔹 Other HTTP Methods

While HTML forms only support GET and POST, other methods are used in web APIs:

🔸 PUT Method

<script>
// Update user profile
fetch('/api/users/123', {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        name: 'John Doe',
        email: '[email protected]'
    })
});
</script>

🔸 DELETE Method

<script>
// Delete a post
fetch('/api/posts/456', {
    method: 'DELETE'
})
.then(response => {
    if (response.ok) {
        console.log('Post deleted successfully');
    }
});
</script>

🔸 PATCH Method

<script>
// Partially update user
fetch('/api/users/123', {
    method: 'PATCH',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        email: '[email protected]'
    })
});
</script>

🔹 Method Override in HTML

Since HTML forms only support GET and POST, you can use hidden fields to simulate other methods:

<!-- Simulate PUT method -->
<form method="POST" action="update-user.php">
    <input type="hidden" name="_method" value="PUT">
    <input type="text" name="name" value="Current Name">
    <input type="submit" value="Update">
</form>

<!-- Simulate DELETE method -->
<form method="POST" action="delete-post.php">
    <input type="hidden" name="_method" value="DELETE">
    <input type="hidden" name="post_id" value="123">
    <input type="submit" value="Delete Post" 
           onclick="return confirm('Are you sure?')">
</form>

🔹 Choosing the Right Method

Use this guide to select the appropriate HTTP method:

Action Method Example
View a page GET Clicking a link
Search GET Search form
Submit form data POST Contact form, login
Upload file POST File upload form
Update resource PUT Edit profile (API)
Delete resource DELETE Remove item (API)

🧠 Test Your Knowledge

Which HTTP method should you use for a login form?