DEV Community

Cover image for Understanding HTTP Methods with Simple Examples 🔻
Ali Samir
Ali Samir

Posted on

Understanding HTTP Methods with Simple Examples 🔻

If you’ve ever interacted with a website or an API, you’ve likely encountered HTTP Methods—the verbs that define the actions we want to perform on resources over the web.

These methods are the backbone of communication between clients (like browsers or mobile apps) and servers.

Let’s dive into the most common HTTP methods with simple examples to make things clear!


📌 What are HTTP Methods?

HTTP methods are like instructions you send to a server, asking it to perform a specific task.

Think of them as commands that tell the server what you want to do with the data it holds. The most commonly used methods are:

  • GET
  • POST
  • PUT
  • DELETE
  • PATCH

Let’s explore each one with real-world examples.


📌 GET: Fetch Data

The GET method is used to retrieve data from the server.

It’s like reading from a book—you’re only looking at the information without making any changes.

Example:

Imagine you’re visiting a blog to read an article.

  • Request:
GET /articles/1 HTTP/1.1
Host: example.com
Enter fullscreen mode Exit fullscreen mode
  • Response:

The server sends back the article details in JSON format:

{
  "id": 1,
  "title": "Understanding HTTP Methods",
  "author": "John Doe"
}
Enter fullscreen mode Exit fullscreen mode

📌 POST: Create Data

The POST method is used to create new resources on the server.

It’s like filling out a form and submitting it.

Example:

You want to create a new blog post.

  • Request:
POST /articles HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "title": "Learn HTTP Methods",
  "author": "Jane Doe"
}
Enter fullscreen mode Exit fullscreen mode
  • Response:

The server confirms that the article was created:

{
  "id": 2,
  "message": "Article created successfully!"
}
Enter fullscreen mode Exit fullscreen mode

📌 PUT: Update Entire Resource

The PUT method is used to update an entire resource.

Think of it as replacing an existing document with a completely new version.

Example:

You want to update an article's content.

  • Request:
PUT /articles/2 HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "title": "Learn HTTP Methods - Updated",
  "author": "Jane Doe"
}
Enter fullscreen mode Exit fullscreen mode
  • Response:
{
  "message": "Article updated successfully!"
}
Enter fullscreen mode Exit fullscreen mode

📌 DELETE: Remove Data

The DELETE method is used to delete a resource from the server.

It’s straightforward—just remove it!

Example:

You decide to delete an article.

  • Request:
DELETE /articles/2 HTTP/1.1
Host: example.com
Enter fullscreen mode Exit fullscreen mode
  • Response:
{
  "message": "Article deleted successfully!"
}
Enter fullscreen mode Exit fullscreen mode

📌 PATCH: Partial Update

The PATCH method is used for a partial update.

Unlike PUT, which requires the entire resource, PATCH lets you update just a specific field.

Example:

You want to change the title of an article.

  • Request:
PATCH /articles/2 HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "title": "Learn HTTP Methods - Patch Example"
}
Enter fullscreen mode Exit fullscreen mode
  • Response:
{
  "message": "Article updated successfully!"
}
Enter fullscreen mode Exit fullscreen mode


📌 Other HTTP Methods

While the methods above are the most common, there are others like:

  • HEAD: Similar to GET but only fetches headers, not the body.
  • OPTIONS: Used to describe the communication options for a resource.
  • CONNECT: For establishing a tunnel to the server.
  • TRACE: Used for diagnostic purposes to trace request routes.

📌 When to Use Which Method?

Method Action Use Case
GET Fetch data Reading an article
POST Create new data Submitting a new article
PUT Replace entire data Updating an article completely
PATCH Modify specific fields Editing just the title
DELETE Remove data Deleting an article

📌 Conclusion

HTTP methods are essential tools for web communication.

Whether you’re building APIs or just trying to understand how the web works, knowing when and how to use these methods is crucial.

Start with the basics—GET and POST—and gradually explore the others as you dive deeper into API development.

Happy coding! 🚀

Top comments (0)