KAHIBARO
Discord Login Register

2.10. HTTP Methods

Overview of HTTP Methods

When a client talks to a server using HTTP, it always sends a request method that says what kind of action it wants to perform on a resource.

Common methods include:

MethodTypical purpose
GETRead / fetch a resource
POSTCreate a new resource or trigger an action
PUTReplace a resource
PATCHPartially update a resource
DELETEDelete a resource
HEADGet headers only, no body
OPTIONSAsk which methods / features are allowed

Every HTTP request line looks like:

text
METHOD /path HTTP/1.1

For example:

text
GET /users HTTP/1.1
Host: example.com

The method is part of the HTTP specification, so servers and clients can agree on what each method is supposed to mean.

Important rule
The HTTP method defines the intent of the request. Backend routes and APIs must be designed so that:

  • URLs identify what you are working with (the resource).
  • Methods describe what you want to do with that resource.

You will use methods constantly when building backend APIs, especially REST APIs later in the course.


Safe, Idempotent, and Cacheable Methods

HTTP methods are not only names. They also have semantics that help with caching, retries, and proxies.

Safe methods

A method is safe if it is meant only to read data, not to change anything on the server.

Standard safe methods:

Safe does not mean there is no effect at all. The server can still log the request or update a counter. It means the method should not create, update, or delete resources in a way that matters to the user.

Example of correct use:

text
GET /products/123

This should not create a new product, change its price, or delete it.

Example of incorrect use:

text
GET /delete-account

If this endpoint actually deletes the account, it breaks the idea of safety. Tools like browsers and crawlers might follow a link and trigger deletion by accident.

Idempotent methods

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

Formally, if $f$ is the effect of a request, then:

$$
f(x) = f(f(x))
$$

Examples:

Methods that are idempotent by design:

MethodIdempotent?Safe?
GETYesYes
HEADYesYes
OPTIONSYesYes
PUTYesNo
DELETEYesNo
POSTNoNo
PATCHNot guaranteedNo

Example of idempotent behavior:

text
DELETE /posts/10

From the point of view of system state, one call or ten calls give the same end result: "no post 10".

Cacheable methods

Some methods are considered cacheable. Intermediaries like browsers and proxies can store responses and reuse them, based on headers.

Common cacheable methods:

POST responses can also be cached if certain headers are present, but this is not common for typical API design.

Example of cacheable usage:

text
GET /products

If the server includes headers like:

text
Cache-Control: max-age=60

then a browser can reuse the cached response for 60 seconds without asking the server again.

As a backend developer, it is important to:

GET

Purpose

GET is used to retrieve information. It does not change data on the server.

Properties:

Examples

Fetch a list of users:

text
GET /users HTTP/1.1
Host: api.example.com

Fetch a single user:

text
GET /users/42 HTTP/1.1
Host: api.example.com

With query parameters (filtering, searching):

text
GET /users?role=admin&page=2 HTTP/1.1
Host: api.example.com

In a browser, when you type a URL like:

text
https://example.com/products

the browser sends:

text
GET /products HTTP/1.1
Host: example.com

GET and request body

By the HTTP standard, GET requests should not have a body. Some clients allow it, but many servers and tools ignore it. For APIs, always put GET parameters in the URL or query string, not in the body.

Bad pattern:

text
GET /search
Body: { "q": "phone" }

Better:

text
GET /search?q=phone

POST

Purpose

POST is used to:

Properties:

Examples: creating resources

Create a new user:

text
POST /users HTTP/1.1
Host: api.example.com
Content-Type: application/json
{
  "name": "Alice",
  "email": "alice@example.com"
}

The server might reply:

text
HTTP/1.1 201 Created
Location: /users/123
Content-Type: application/json
{
  "id": 123,
  "name": "Alice",
  "email": "alice@example.com"
}

The important part is that POST /users does not say what the ID will be. The server creates it.

Examples: actions

Sometimes an operation does not fit "create exactly one resource". Then POST is still used.

Examples:

In these cases, POST means "perform this action using the submitted data".

Non-idempotent behavior

Consider:

text
POST /orders
Body: { "product_id": 10, "quantity": 1 }

If you send it twice by mistake, you may get:

So sending the same POST more than once usually changes the state more than once.


PUT

Purpose

PUT is used to replace an entire resource at a specific URL.

Properties:

The key idea is that the client sends the full representation of the resource and says: "make the resource at this URL exactly like this".

Examples

Replace a user:

text
PUT /users/123 HTTP/1.1
Content-Type: application/json
{
  "id": 123,
  "name": "Alice Smith",
  "email": "alice@example.com",
  "role": "admin"
}

If the server already has a user 123, it will update it to match this JSON exactly. If some field is missing from the body, the server may set it to a default or null, depending on implementation.

Calling the same PUT again with the same data does nothing new, because the user is already in that state.

Creating or not creating on PUT

Different APIs handle this differently:

The HTTP specification allows both, but you should be consistent in your own API.

PUT vs POST

A simple way to remember the difference:

Another mental model:


QuestionPOSTPUT
Who chooses the resource URLServerClient
Typical useCreate, actionsReplace a specific resource
IdempotentNoYes

PATCH

Purpose

PATCH is used to partially update a resource, not replace it completely.

Properties:

PATCH is useful when you only want to send the fields that change.

Examples: partial update

Suppose a user has this full data:

json
{
  "id": 123,
  "name": "Alice Smith",
  "email": "alice@example.com",
  "role": "user",
  "active": true
}

You want to change only the role:

text
PATCH /users/123 HTTP/1.1
Content-Type: application/json
{
  "role": "admin"
}

A reasonable server behavior:

Repeat the same PATCH:

text
PATCH /users/123
{ "role": "admin" }

The second time, the user is already admin, so state does not change. In this case, it behaves idempotently, even though the spec does not require it.

JSON Patch and other formats

There is a specific format called JSON Patch defined in RFC 6902. It uses an array of operations like "replace this path" or "add to that path".

Example JSON Patch body:

json
[
  { "op": "replace", "path": "/role", "value": "admin" },
  { "op": "add", "path": "/tags/-", "value": "vip" }
]

Then:

text
PATCH /users/123 HTTP/1.1
Content-Type: application/json-patch+json
[ ... ]

Not all APIs use JSON Patch. Many use a simpler "partial object" style, where you send only the fields to update.


DELETE

Purpose

DELETE is used to remove a resource.

Properties:

Examples

Delete a specific user:

text
DELETE /users/123 HTTP/1.1
Host: api.example.com

Server responses can vary:

Some APIs always return 204 even if it was already deleted, to keep DELETE idempotent.

Soft delete vs hard delete

Internally, servers often do not physically remove data. They can:

From the HTTP perspective, both are "delete". The difference is an internal implementation detail.


HEAD

Purpose

HEAD is like GET, but the server returns only headers, no body.

Properties:

This is useful to check:

Examples

Check if a file exists:

text
HEAD /files/report.pdf HTTP/1.1
Host: example.com

Server response:

text
HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Length: 582131
ETag: "abc123"

No body is sent, so the response is smaller and faster.

Use cases:

OPTIONS

Purpose

OPTIONS asks the server: "What can I do with this resource or URL?"

Properties:

Examples

General options for the server:

text
OPTIONS * HTTP/1.1
Host: example.com

Options for a specific resource:

text
OPTIONS /users/123 HTTP/1.1
Host: api.example.com

The server might respond:

text
HTTP/1.1 204 No Content
Allow: GET, PUT, PATCH, DELETE, OPTIONS

The Allow header lists the supported methods.

OPTIONS and CORS

In browsers, OPTIONS is heavily used with CORS (Cross Origin Resource Sharing). When a web page sends certain types of cross origin requests, the browser may send a preflight request:

text
OPTIONS /api/data HTTP/1.1
Origin: https://myapp.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: Content-Type

The server then replies with headers that tell the browser whether the real request is allowed.

You will see and configure this often in backend work for APIs used by browsers.


Method Semantics and Good API Design

In backend development, you often need to choose which method to use for an endpoint. Here are some guiding principles.

Use the correct method for what you do

Typical REST style mapping:

ActionPath exampleMethod
List all items/usersGET
Get one item/users/123GET
Create a new item/usersPOST
Replace an item completely/users/123PUT
Partially update an item/users/123PATCH
Delete an item/users/123DELETE

Avoid patterns like:

text
GET /create-user
GET /delete-user?id=123

They break the semantics of GET and can cause security and caching issues.

Think about idempotency and retries

Network requests can fail. Clients, proxies, or load balancers may retry requests.

Example problem:

Solutions (later in the course) include idempotency keys, but for now remember:

Key idea
Non idempotent methods like POST can create side effects multiple times if retried. Idempotent methods like PUT and DELETE are designed so that multiple retries lead to the same final state.

Document supported methods

Your API should:

Example:

text
HTTP/1.1 405 Method Not Allowed
Allow: GET, POST

Summary

You will apply these concepts directly when you start building APIs and backend routes in later chapters.

Views: 8

Comments

Please login to add a comment.

Don't have an account? Register now!