Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Understanding HTTP Methods

Recommended resources

Hello! In our last lesson, we traced the entire journey of an HTTP request, from a browser initiating a connection to a server processing the request and sending back a response. A key part of that request was the HTTP method, which acts as a verb telling the server what action to perform.

Given your extensive experience with API testing in Postman, you've likely used these methods—GET, POST, PUT, and others—countless times. This lesson will move beyond their practical application to explore their formal definitions, intended uses, and the underlying architectural principles that govern them. Mastering this will be crucial for you to analyze system designs and contribute to architectural discussions, as it forms the semantic foundation of any RESTful API.

Our goal today is to differentiate between the primary HTTP methods (GET, POST, PUT, DELETE, PATCH) and understand their specific roles in creating predictable, scalable, and robust web services.

The Verbs of the Web: A CRUD Overview

At a high level, the most common HTTP methods map directly to the four basic operations of persistent storage: Create, Read, Update, and Delete (CRUD). This provides a simple starting point for understanding their purpose.

This diagram shows the mapping of common HTTP methods to their corresponding CRUD operations on a resource.

While this mapping is a useful guide, the real distinctions between these methods lie in two fundamental properties: safety and idempotency. These are not just academic terms; they are core architectural concepts that have profound implications for system reliability, fault tolerance, and performance.


Core Principles: Safety and Idempotency

Before we examine each method, it's essential to understand these two properties.

PUT vs POST vs PATCH: Choosing the Right HTTP Method

The article "PUT vs POST vs PATCH" begins by defining these foundational concepts clearly.

Please read the introductory section, What is Safety?, to understand the formal definitions of idempotence and safety.

To summarize:

  • Safety: A method is safe if it does not alter the state of the resource on the server. Safe methods are essentially "read-only." GET is a prime example. This property allows clients, proxies, and search engine crawlers to make GET requests without fear of causing unintended side effects.
  • Idempotency: An operation is idempotent if making the same request multiple times produces the same result as making it once. This is a critical property for building reliable systems. If a client sends an idempotent request and a network error occurs, it can safely retry the request without worrying about creating duplicate entries or performing an update multiple times.

The Venn diagram below illustrates how the common HTTP methods fall into these categories. Note that GET and other safe methods are inherently idempotent.

A Venn diagram showing how HTTP methods are classified based on the properties of safety and idempotency.

Now, let's explore each method through the lens of these principles.


A Method-by-Method Deep Dive

We will now examine the five most common HTTP methods used in RESTful APIs.

1. GET: The Reader

The GET method is used to retrieve a representation of a resource.

  • Purpose: Read data.
  • Safety: Safe. It should not change any data on the server.
  • Idempotency: Idempotent. Calling GET /users/123 ten times will return the same data each time (unless another method, like PUT, has changed it in the meantime).
  • Body: A GET request should not have a request body.

Because GET is safe, responses to GET requests are highly cacheable by browsers and intermediate network proxies, which is a key strategy for improving system performance.

2. POST: The Creator

The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server. Its most common use is to create a new subordinate resource.

  • Purpose: Create a new resource, or submit data for processing.
  • Safety: Not Safe. It creates a new resource, changing the server's state.
  • Idempotency: Not Idempotent. If you send the same POST /users request twice, you will create two distinct users. This is a crucial distinction. Since it's not idempotent, a client cannot automatically retry a POST request on network failure.
  • Body: Contains the data for the new resource or the data to be processed.

Example: POST /users with a JSON body containing user details. The server creates the user and typically responds with a 201 Created status and a Location header pointing to the new resource's URL (e.g., /users/124).

3. PUT: The Replacer

The PUT method replaces an existing resource with the data provided in the request body. If the resource does not exist at the specified URI, the server may create it.

  • Purpose: Replace an entire resource or create one at a specific, client-defined URI.
  • Safety: Not Safe. It modifies a resource on the server.
  • Idempotency: Idempotent. If you send PUT /users/123 with the same payload multiple times, the final state of user 123 will be identical to the result of the first request. The resource is simply replaced each time. This makes PUT a reliable choice for update operations that can be retried.
  • Body: Contains the complete new representation of the resource.

The key difference from POST is that PUT is idempotent and acts on a specific URI (e.g., PUT /users/123), whereas POST is not idempotent and acts on a collection URI (e.g., POST /users).

4. PATCH: The Modifier

The PATCH method is used to apply partial modifications to a resource.

  • Purpose: Partially update an existing resource.
  • Safety: Not Safe. It modifies a resource.
  • Idempotency: Not necessarily idempotent. Its idempotency depends on the nature of the update. An instruction like "set the user's email to 'a@b.com'" is idempotent. However, an instruction like "add 10 to the user's login count" is not. Therefore, PATCH is considered conditionally idempotent.
  • Body: Contains a set of instructions describing the changes to be made (a "patch document").

PATCH is more efficient than PUT for large resources because you only need to send the data that is changing, not the entire resource representation.

5. DELETE: The Destroyer

The DELETE method deletes the specified resource.

  • Purpose: Delete a resource.
  • Safety: Not Safe. It removes a resource, changing the server's state.
  • Idempotency: Idempotent. If you send DELETE /users/123 once, the user is deleted. If you send it again, the system state remains the same: the user is still gone. The server will likely respond with 204 No Content on the first success and 404 Not Found on subsequent attempts, but the end result on the resource state is identical.

Consolidating Your Understanding

The following video provides an excellent review of these methods, reinforcing their characteristics and best practices.

Mastering HTTP Methods: A REST API Deep Dive

The video "Mastering HTTP Methods: A REST API Deep Dive" by ByteMonk offers a clear, practical walkthrough of each method.

Watch the segments covering GET, POST, PUT, PATCH, and DELETE. The video also briefly covers HEAD and OPTIONS, which are useful to be aware of.

The distinction between PUT and POST is a classic point of confusion. For a deeper dive, the next video provides historical context that clarifies why these methods were designed the way they were.

PUT vs POST: What's the difference?

In "PUT vs POST: What's the difference?", Cameron McKenzie explains the evolution of the HTTP protocol.

Watch the sections that cover the history of HTTP methods, the concepts of safety and idempotence, and the final summary of their roles. This historical perspective is invaluable for understanding the intent behind the specification.


From QA to Architecture: Common Anti-Patterns

Your background in QA gives you a unique advantage in understanding system architecture: you are trained to think about edge cases and what happens when things go wrong. Choosing the wrong HTTP method is a frequent source of bugs and brittle API design.

PUT vs POST vs PATCH: Choosing the Right HTTP Method

Let's revisit the article "PUT vs POST vs PATCH" to review common mistakes developers make when implementing these methods.

Read the section titled Recognizing Anti-Patterns. Pay close attention to the consequences described for "Using POST for Everything" and "Using GET to Change State," as these are issues you may have encountered during testing.

Understanding these anti-patterns moves you from simply using an API to critiquing its design, a key step toward your goal of participating in architectural discussions.


Conclusion

In this lesson, we've formalized the definitions and intended uses of the primary HTTP methods. This knowledge goes beyond simple API testing; it's about communicating intent and building systems that are predictable, reliable, and scalable.

Here are the key takeaways:

  • HTTP methods correspond to CRUD operations but are more formally defined by their properties of safety and idempotency.
  • GET is safe and idempotent, used for retrieval.
  • POST is neither safe nor idempotent, used for creating resources where the server defines the URL.
  • PUT is not safe but is idempotent, used for replacing a resource at a client-defined URL.
  • PATCH is not safe and conditionally idempotent, used for efficient partial updates.
  • DELETE is not safe but is idempotent, used for removing a resource.
  • Adhering to these semantics is critical for correct caching behavior, network fault tolerance, and creating intuitive, robust APIs.

In our next lesson, we will explore the server's side of the conversation by examining HTTP status codes. Just as the method tells the server what we want, the status code tells us how the server responded, providing essential feedback for application logic and error handling.