HTTP Methods Overview
HTTP methods (also called HTTP verbs) define the action to perform on a resource. REST uses these methods to create a uniform interface for all operations.
The Main HTTP Methods
| Method | Purpose | Request Body | Response Body | Idempotent | Safe |
|---|---|---|---|---|---|
| GET | Retrieve resource | No | Yes | Yes | Yes |
| POST | Create resource | Yes | Yes | No | No |
| PUT | Replace resource | Yes | Yes | Yes | No |
| PATCH | Partial update | Yes | Yes | No* | No |
| DELETE | Remove resource | No | Optional | Yes | No |
*PATCH can be idempotent depending on implementation
GET - Retrieve Resources
GET requests retrieve a resource or collection without modifying anything.
GET /api/users HTTP/1.1
Host: api.example.com
Accept: application/json
Response:
HTTP/1.1 200 OK
Content-Type: application/json
[
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
Key Points:
- Never modifies data (safe)
- Can be cached
- Should be repeatable without side effects
- No request body
POST - Create Resources
POST creates a new resource. The server typically assigns the ID.
POST /api/users HTTP/1.1
Content-Type: application/json
{
"name": "Charlie",
"email": "charlie@example.com"
}
Response:
HTTP/1.1 201 Created
Location: /api/users/3
Content-Type: application/json
{
"id": 3,
"name": "Charlie",
"email": "charlie@example.com"
}
Key Points:
- Creates new resources
- Returns 201 Created on success
- Include Location header with new resource URL
- Not idempotent (calling twice creates two resources)
PUT - Replace Resources
PUT replaces an entire resource. If it doesn't exist, it may create it.
PUT /api/users/3 HTTP/1.1
Content-Type: application/json
{
"name": "Charlie Updated",
"email": "charlie.new@example.com",
"role": "admin"
}
Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 3,
"name": "Charlie Updated",
"email": "charlie.new@example.com",
"role": "admin"
}
Key Points:
- Replaces the entire resource
- Include all fields (missing fields may be set to null)
- Idempotent (calling twice has same result as calling once)
- Client specifies the resource ID
PATCH - Partial Updates
PATCH applies partial modifications to a resource.
PATCH /api/users/3 HTTP/1.1
Content-Type: application/json
{
"role": "moderator"
}
Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 3,
"name": "Charlie Updated",
"email": "charlie.new@example.com",
"role": "moderator"
}
Key Points:
- Only send fields that need updating
- Other fields remain unchanged
- More efficient than PUT for small changes
DELETE - Remove Resources
DELETE removes a resource.
DELETE /api/users/3 HTTP/1.1
Response:
HTTP/1.1 204 No Content
Key Points:
- Idempotent (deleting twice returns same result)
- Typically returns 204 No Content
- Can return 200 OK with deleted resource info
Exercise: Choose the Right Method
Less Common Methods
HEAD
Like GET but returns only headers, no body. Useful for checking if a resource exists.
HEAD /api/users/123 HTTP/1.1
OPTIONS
Returns allowed methods and CORS information.
OPTIONS /api/users HTTP/1.1
HTTP/1.1 200 OK
Allow: GET, POST, HEAD, OPTIONS
Summary
| Method | Use When... |
|---|---|
| GET | You want to retrieve data without changing anything |
| POST | You're creating a new resource |
| PUT | You're replacing an entire resource |
| PATCH | You're updating part of a resource |
| DELETE | You're removing a resource |
Choosing the right HTTP method is fundamental to RESTful design. In the next lesson, we'll see how these methods map to CRUD operations.
التالي · الدرس 8 من 37
CRUD Operations with HTTP
قراءة 4 دقيقة
أنهيت 7 من 37 درسًا

اسأل Byte عن هذا الدرس
محادثة خاصة حول هذا الدرس. قرأ Byte الدرس كاملًا، ويجيب خلال ثوانٍ، ويُظهر القسم الذي تأتي منه كل إجابة. يمكنك السؤال بأي لغة.
الأسئلة والأجوبة
أسئلة عامة حول هذا الدرس. يجيب AI Tutor فورًا بلغتك، ويراجع فريق FreeAcademy الإجابات، ويمكن لكل المتعلمين قراءتها.

