KAHIBARO
Discord Login Register

7.5. HTTP Methods in REST

Why HTTP Methods Matter in REST

In a REST API, the URL (path) identifies what you are working with, and the HTTP method tells the server what you want to do with it.

For example, these two requests mean very different things, even though they use the same URL:

http
GET  /users/123
DELETE /users/123

In REST, methods carry meaning. If you use them correctly, your API becomes predictable and easy to understand.

Key rule: In REST, you should use HTTP methods according to their standard semantics:

  • GET to read.
  • POST to create or perform a non-idempotent action.
  • PUT to fully replace a resource.
  • PATCH to partially update a resource.
  • DELETE to remove a resource.

You already have separate chapters for each method, so this chapter focuses on:

CRUD and HTTP Methods

Backend systems often think in terms of CRUD:

CRUD actionDescriptionTypical HTTP methods
CreateAdd a new resourcePOST, sometimes PUT
ReadRetrieve existing resource(s)GET, sometimes HEAD
UpdateChange existing resourcePUT, PATCH
DeleteRemove resourceDELETE

A common REST pattern for a resource called users looks like this:

OperationHTTP methodURL
List usersGET/users
Get a single userGET/users/{id}
Create a new userPOST/users
Replace a userPUT/users/{id}
Partially update a userPATCH/users/{id}
Delete a userDELETE/users/{id}

You will see this table repeatedly in real APIs such as GitHub, Stripe, or many internal company APIs.


Safety and Idempotency

Two important concepts help you choose the right method: safety and idempotency.

Safety

A method is safe if it is not supposed to change server state.

Safe methods should only read data, not create, update, or delete.

Examples:

Rule: GET must not have side effects like creating, updating, or deleting data.

Idempotency

A method is idempotent if making the same request multiple times in a row has the same effect as making it once.

Examples:

This does not mean the response is always the same, only that the final state of the resource does not change after the first successful call.

Common methods and idempotency:

MethodSafeIdempotentTypical use
GETYesYesRead data
HEADYesYesRead metadata (headers only)
OPTIONSYesYesDiscover capabilities
POSTNoNoCreate resources, non-idempotent actions
PUTNoYesReplace a resource
PATCHNoNot guaranteedPartially modify a resource
DELETENoYesDelete a resource

Rule: POST is not idempotent, PUT and DELETE should be idempotent, GET should be both safe and idempotent.

Why this matters:

Overview of Key HTTP Methods in REST

You have separate chapters for each of these, so here we will focus on how they fit together and how to choose between them.

GET

Used to retrieve data.

Examples:

http
GET /users
GET /users/42
GET /posts?author_id=42&page=2

Common misuse:

POST

Used to create a resource or start an action that is not idempotent.

Examples:

http
POST /users
POST /orders
POST /orders/123/cancel
POST /reports/generate

Typical behavior:

POST is flexible:

PUT

Used to fully replace a resource at a known URL.

Examples:

http
PUT /users/123
PUT /settings/global

In PUT /users/123, the body contains the entire representation of the user, not just the fields you want to change.

Key properties:

Example: client chooses the ID:

http
PUT /users/alice
Content-Type: application/json
{
  "id": "alice",
  "name": "Alice Smith",
  "email": "alice@example.com"
}

If /users/alice does not exist, the server may create it. If it exists, the server replaces its data.

PATCH

Used to partially update an existing resource.

Examples:

http
PATCH /users/123
Content-Type: application/json
{
  "email": "new-email@example.com"
}

Comparison:

DELETE

Used to remove a resource.

Examples:

http
DELETE /users/123
DELETE /posts/99/comments/5

Designing REST APIs with Methods

Picking the correct method for each endpoint is part of designing a RESTful API.

Common Resource Patterns

Imagine you design an API for a simple blogging system with:

Users

ActionMethodURLNotes
List usersGET/usersOptional filters as query params
Create userPOST/usersBody contains user data
Get single userGET/users/{id}
Replace userPUT/users/{id}Full user body
Partially update userPATCH/users/{id}Only changed fields
Delete userDELETE/users/{id}

Posts

ActionMethodURL
List postsGET/posts
Create postPOST/posts
Get single postGET/posts/{id}
Update post fullyPUT/posts/{id}
Update post partiallyPATCH/posts/{id}
Delete postDELETE/posts/{id}
List comments of a postGET/posts/{id}/comments
Add comment to a postPOST/posts/{id}/comments

Custom actions

Sometimes you need an action that does not fit simple CRUD, for example liking a post.

Bad design:

Better designs:

Both approaches use methods in a way that respects their semantics: an action that changes state should not be GET.


Choosing Between POST, PUT, and PATCH

These three methods are often confusing. Here are some practical guidelines.

When to Use POST

Use POST when:

Examples:

http
POST /users           # Server decides user id
POST /orders          # Server decides order id
POST /posts/123/comments  # Server decides comment id

Also use POST for:

Examples:

http
POST /auth/login
POST /payments/123/refund
POST /reports/generate

When to Use PUT

Use PUT when:

Examples:

http
PUT /users/123
PUT /profiles/john-doe
PUT /settings/global

Decide on a rule:

Be consistent in your API.

When to Use PATCH

Use PATCH when:

Examples:

http
PATCH /users/123
{
  "name": "New Name"
}
PATCH /users/123
{
  "is_active": false
}

You will define how the server interprets the payload. A common rule is:

Idempotency in Real Scenarios

Idempotency is very important for retries. For example, a client might send a request, get a timeout, and try again. If the method is idempotent, this is safer.

Idempotent Example: PUT

Imagine you have:

http
PUT /accounts/123/balance
Content-Type: application/json
{ "balance": 1000 }

Whether this is called once or five times, the final balance is 1000.

Non Idempotent Example: POST

Imagine:

http
POST /orders
Content-Type: application/json
{ "item_id": 42, "quantity": 1 }

If this is called twice because of a timeout, the user might receive two orders. This is correct behavior, because POST is non idempotent.

To protect from this in real systems, APIs sometimes use:

Common Design Mistakes and How to Avoid Them

Misusing GET for state changes

Bad:

http
GET /users/123/activate

This changes the user from inactive to active, which violates the safety of GET.

Better:

http
POST /users/123/activate

or a more RESTful resource-oriented design:

http
POST   /users/123/activation   # create an activation
DELETE /users/123/activation   # remove activation (deactivate)

Using POST for everything

Some APIs use only POST and GET, for example:

http
POST /updateUser
POST /deleteUser
POST /getUser

This might work, but the API:

Instead, use:

http
GET    /users/123
PUT    /users/123
DELETE /users/123

Confusing PUT and PATCH

If you design PUT /users/123 to accept partial updates, then you blur the line:

Better:

Practical Examples

Here are some complete request and response examples to see methods in context.

Creating a resource with POST

http
POST /users
Content-Type: application/json
{
  "name": "Alice",
  "email": "alice@example.com"
}

Possible response:

http
HTTP/1.1 201 Created
Location: /users/123
Content-Type: application/json
{
  "id": 123,
  "name": "Alice",
  "email": "alice@example.com",
  "created_at": "2026-08-27T10:00:00Z"
}

Reading the created resource with GET

http
GET /users/123
Accept: application/json

Response:

http
HTTP/1.1 200 OK
Content-Type: application/json
{
  "id": 123,
  "name": "Alice",
  "email": "alice@example.com",
  "created_at": "2026-08-27T10:00:00Z"
}

Fully replacing the user with PUT

http
PUT /users/123
Content-Type: application/json
{
  "id": 123,
  "name": "Alice Smith",
  "email": "alice.smith@example.com"
}

This request overwrites the user. Any fields not sent might be set to default or removed, depending on your API design.

Partially updating the user with PATCH

http
PATCH /users/123
Content-Type: application/json
{
  "name": "Alice Johnson"
}

Only the name changes. Other fields remain unchanged.

Deleting the user with DELETE

http
DELETE /users/123

Response:

http
HTTP/1.1 204 No Content

If the client sends DELETE /users/123 again, the server may return:

http
HTTP/1.1 404 Not Found

The state of the system is still the same as after the first delete, so the method remains idempotent.


Summary

In the following chapters, you will dive into each method in detail and see how to implement and use them in real backend code.

Views: 9

Comments

Please login to add a comment.

Don't have an account? Register now!