2026-08-21 14:58:46.105145+08 / restful-api

HTTP Requests

Guidelines for HTTP method semantics, safe and idempotent operations, query parameters, complex operations, and asynchronous request tracking.

Restful ApiApi DesignOpenapi

12. HTTP Requests

RESTful APIs use standard HTTP methods to define operations on resources. API implementations MUST follow the standard semantics of HTTP methods to ensure predictable behavior, interoperability, and correct client/server interaction.

HTTP Method Semantics

GET

GET requests are used to retrieve resources, either individual resources or collections.

Rules:

  • GET requests for individual resources should return 404 Not Found if the resource does not exist.

  • GET requests for collection resources may return:

    • 200 OK when the collection exists but contains no elements.
    • 404 Not Found when the collection itself does not exist.
  • GET requests MUST NOT contain a request body.

Example:

text
GET /customers/123

Response:

http
HTTP/1.1 200 OK
json
{
  "id": "123",
  "name": "John Smith"
}

For collection resources, APIs SHOULD provide filtering and pagination mechanisms.

Example:

text
GET /customers?status=active&page=1&pageSize=20

PUT

PUT requests are used to replace an entire resource representation.

The semantic is:

Replace the resource identified by the URL with the representation provided in the request payload.

Example:

text
PUT /customers/123

Request:

json
{
  "id": "123",
  "name": "John Smith",
  "status": "active"
}

Rules:

  • PUT is normally used for individual resources.

  • The request payload represents the complete resource.

  • Existing resource data is replaced by the new representation.

  • Unknown fields SHOULD be ignored rather than stored.

  • Successful responses usually return:

    • 200 OK
    • 204 No Content
    • 201 Created when a new resource is created.

Example:

http
HTTP/1.1 204 No Content

PUT and Resource Creation

Although PUT may create resources, POST SHOULD normally be preferred for creating top-level resources.

When PUT is used for creation:

  • The client controls the resource identifier.
  • Repeating the same request must be idempotent.

Example:

text
PUT /customers/customer123

The following two requests should create the same result:

text
PUT /customers/customer123
PUT /customers/customer123

APIs MAY use ETag with conditional headers such as:

text
If-Match
If-None-Match

to prevent concurrent update conflicts.


POST

POST requests are primarily used to create new resources under a collection.

The semantic is:

Add the enclosed representation to the collection identified by the URL.

Example:

text
POST /customers

Request:

json
{
  "name": "John Smith",
  "status": "active"
}

Response:

http
HTTP/1.1 201 Created
Location: /customers/123
json
{
  "id": "123",
  "name": "John Smith",
  "status": "active"
}

Rules:

  • The server normally generates the resource identifier.

  • The response SHOULD provide the created resource URI.

  • Common response codes:

    • 201 Created — resource successfully created.
    • 202 Accepted — request accepted for asynchronous processing.
    • 200 OK — resource updated or operation completed.
    • 204 No Content — successful operation without response body.

Unlike PUT, repeating the same POST request is not guaranteed to produce the same result.

Example:

text
POST /orders

May create:

text
Order A
Order B

for two identical requests.

For important scenarios, APIs SHOULD consider making POST operations idempotent.


PATCH

PATCH requests are used to partially update an existing resource.

The semantic is:

Modify the resource identified by the URL according to the changes described in the request.

Example:

text
PATCH /customers/123

Request:

json
{
  "status": "inactive"
}

Result:

json
{
  "id": "123",
  "name": "John Smith",
  "status": "inactive"
}

Rules:

  • PATCH normally applies to individual resources.

  • PATCH usually requires the resource to already exist.

  • Only specified fields are updated.

  • Successful responses usually return:

    • 200 OK
    • 204 No Content

Recommended PATCH Patterns

To avoid inconsistent implementations, APIs SHOULD select one update pattern per endpoint.

Recommended order:

1. Prefer PUT when possible

Use full resource replacement:

text
PUT /customers/123

with the complete object.


2. JSON Merge Patch

Use partial objects:

Content-Type:

text
application/merge-patch+json

Example:

json
{
  "status": "inactive"
}

3. JSON Patch

Use operation-based updates:

Content-Type:

text
application/json-patch+json

Example:

json
[
  {
    "op": "replace",
    "path": "/status",
    "value": "inactive"
  }
]

4. Use POST for special operations

If the update behavior cannot be represented by PATCH semantics, use POST and document the behavior clearly.


DELETE

DELETE requests remove resources.

The semantic is:

Delete the resource identified by the URL.

Example:

text
DELETE /customers/123

Successful responses:

text
HTTP/1.1 204 No Content

or:

text
HTTP/1.1 200 OK

with deleted resource information.

Rules:

  • DELETE normally applies to individual resources.
  • Deleting a collection resource is discouraged because it implies deleting the entire collection.
  • After deletion, subsequent GET requests should return:
text
404 Not Found

or:

text
410 Gone

The deleted resource MUST NOT remain accessible.


HEAD

HEAD has the same semantics as GET, but returns only HTTP headers.

Example:

text
HEAD /customers/123

Response:

http
HTTP/1.1 200 OK
Content-Length: 1024
ETag: "abc123"

Typical usage:

  • Check whether a resource exists.
  • Check metadata.
  • Validate cache information.

OPTIONS

OPTIONS is used to discover supported operations for an endpoint.

Example:

text
OPTIONS /customers/123

Response:

http
Allow: GET, PUT, DELETE

It may also describe available operations or supported capabilities.


HTTP Method Properties

HTTP methods have three important characteristics:

Safe

A safe operation does not intentionally modify server state.

Examples:

Safe:

  • GET
  • HEAD
  • OPTIONS

Not safe:

  • POST
  • PUT
  • PATCH
  • DELETE

Idempotent

An idempotent operation produces the same intended server state when executed multiple times.

Examples:

Method Idempotent
GET Yes
HEAD Yes
PUT Yes
DELETE Yes
POST No
PATCH Not guaranteed

Example:

text
PUT /customers/123

Executing:

text
PUT /customers/123
PUT /customers/123

results in the same resource state.


Cacheable

Responses may be stored and reused.

Typical cacheable methods:

  • GET
  • HEAD

Some POST responses may be cacheable if explicitly defined.


Query Parameters

Query parameters SHOULD be used for resource filtering and simple queries.

Example:

text
GET /customers?name=John

Filtering definitions SHOULD specify:

  • Related resource property.
  • Allowed value range.
  • Comparison behavior.
  • Combination rules.

Examples:

Equality:

text
name=John

Range:

text
ageGreaterThan=18

Time filtering:

text
createdBefore=2026-01-01

Complex Operations

Complex operations SHOULD be modeled as resources instead of adding verbs into URLs.

Avoid:

text
POST /customers/searchCustomers

Prefer:

text
POST /customers/searches

Request:

json
{
  "query": "complex search criteria"
}

Response:

http
HTTP/1.1 201 Created
json
{
  "id": "search123",
  "results": {
    "items": []
  }
}

For asynchronous processing:

http
HTTP/1.1 202 Accepted
Location: /customers/searches/search123

Client can later query:

text
GET /customers/searches/search123

Asynchronous Request Tracking

When an API returns:

text
202 Accepted

the response SHOULD provide a way to track execution status.

Example:

Request:

text
POST /customers

Response:

http
HTTP/1.1 202 Accepted
json
{
  "id": "123",
  "meta": {
    "status": "pending"
  }
}

Recommended status values:

text
queued
pending
processing
cancelled
failed
completed

The client can poll:

text
GET /customers/123

until completion.

Alternative:

Return a dedicated job resource:

json
{
  "job": {
    "id": "456",
    "href": "/jobs/456",
    "status": "processing"
  }
}

Then query:

text
GET /jobs/456

Response:

json
{
  "id": "456",
  "status": "completed",
  "customer": {
    "href": "/customers/123"
  }
}

Summary

Method Purpose Safe Idempotent
GET Read resource Yes Yes
HEAD Read metadata Yes Yes
POST Create resource / execute operation No No
PUT Replace resource No Yes
PATCH Partial update No Not guaranteed
DELETE Delete resource No Yes
OPTIONS Discover capabilities Yes Yes

REST API design should rely on standard HTTP semantics rather than creating custom action-style endpoints. This improves predictability, interoperability, caching behavior, and long-term API evolution.

HTTP Requests | Remi Resume