Which methods to use (get/post/put…)

YASE - Yet Another Software Engineer

Which methods to use (get/post/put…)

Unlike other types of APIs, such as RPC APIs, REST APIs use standard HTTP methods to indicate the type of operation to be performed on a resource, data, database entity, or code object. For example, if we want to add a new record to the “orders” table in the database, we would code a function that, upon a POST /v1/order request, adds a new order with that ID to the database (along with any other parameters in the body of the POST request).

Why did we use the POST method specifically, and not, say, PUT?

Well, each HTTP method, or at least the main ones, has a specific meaning in the context of REST APIs. Let’s briefly look at which methods to use based on the type of operation we want to perform on resources.

PREMISE: an operation (like an HTTP request) is idempotent if performing it multiple times produces exactly the same end result as performing it just once.

Basically: it doesn’t matter if you make the same request once or 10 times in a row; the system’s state will be identical to the state after the first successful execution.

In the context of HTTP methods (used in REST APIs), a ‘safe’ operation or method is defined as one that is not intended to change the state of the resource on the server.