Tan Phat Media

HTTP Method Reference - Tra Cứu HTTP Methods

Tra cứu đầy đủ các HTTP methods với ví dụ và best practices

Search Methods
GET
Safe
Idempotent
Cacheable

Description

Retrieve data from server. Should only retrieve data and have no other effect.

Use Case

Fetching resources, reading data, search queries

Example Request

GET /api/users/123 HTTP/1.1
Host: api.example.com
Accept: application/json

Common Status Codes

200 OK
404 Not Found
304 Not Modified
POST

Description

Submit data to server to create a new resource. Often causes state change or side effects.

Use Case

Creating resources, submitting forms, uploading files

Example Request

POST /api/users HTTP/1.1
Host: api.example.com
Content-Type: application/json

{
  "name": "John Doe",
  "email": "john@example.com"
}

Common Status Codes

201 Created
200 OK
400 Bad Request
PUT
Idempotent

Description

Replace all current representations of target resource with request payload.

Use Case

Updating entire resource, replacing data

Example Request

PUT /api/users/123 HTTP/1.1
Host: api.example.com
Content-Type: application/json

{
  "name": "John Smith",
  "email": "john.smith@example.com"
}

Common Status Codes

200 OK
204 No Content
404 Not Found
PATCH

Description

Apply partial modifications to a resource.

Use Case

Partial updates, modifying specific fields

Example Request

PATCH /api/users/123 HTTP/1.1
Host: api.example.com
Content-Type: application/json

{
  "email": "newemail@example.com"
}

Common Status Codes

200 OK
204 No Content
404 Not Found
DELETE
Idempotent

Description

Delete the specified resource.

Use Case

Removing resources, deleting data

Example Request

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

Common Status Codes

200 OK
204 No Content
404 Not Found
HEAD
Safe
Idempotent
Cacheable

Description

Same as GET but returns only headers, no body. Used to check if resource exists.

Use Case

Checking resource existence, getting metadata

Example Request

HEAD /api/users/123 HTTP/1.1
Host: api.example.com

Common Status Codes

200 OK
404 Not Found
OPTIONS
Safe
Idempotent

Description

Describe communication options for target resource. Used for CORS preflight.

Use Case

CORS preflight, discovering allowed methods

Example Request

OPTIONS /api/users HTTP/1.1
Host: api.example.com
Access-Control-Request-Method: POST

Common Status Codes

200 OK
204 No Content

HTTP Method Reference - Tra Cứu HTTP Methods Đầy Đủ

HTTP Method Reference là bảng tra cứu đầy đủ các HTTP methods (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS) với hướng dẫn sử dụng, ví dụ code, status codes và best practices cho RESTful API development.

Tính năng nổi bật

Tra cứu 7 HTTP methods chính
GET - Retrieve data từ server
POST - Create new resources
PUT - Update entire resource
PATCH - Partial updates
DELETE - Remove resources
HEAD - Get headers only
OPTIONS - CORS preflight
Ví dụ HTTP request cho mỗi method
Status codes phổ biến
Idempotent và safe properties
Cacheable information
Use cases thực tế
Copy examples một click
Search và filter nhanh

Tại sao cần HTTP Method Reference?

HTTP methods là foundation của RESTful APIs và web communication. Hiểu rõ từng method, khi nào dùng GET vs POST, PUT vs PATCH, và properties như idempotent, safe, cacheable là essential cho API design. HTTP Method Reference giúp developers tra cứu nhanh, học best practices, và implement APIs đúng chuẩn. Đặc biệt hữu ích khi design RESTful APIs, debug HTTP requests, hoặc học web development.

Lợi ích khi sử dụng

  • Tra cứu HTTP methods nhanh chóng
  • Hiểu rõ idempotent và safe properties
  • Học RESTful API best practices
  • Ví dụ code thực tế sẵn sàng sử dụng
  • Biết status codes phù hợp cho mỗi method
  • Tránh sai lầm khi design APIs
  • Reference cho interviews và learning
  • Miễn phí, không cần đăng ký

Cách sử dụng HTTP Method Reference

  1. 1Mở HTTP Method Reference trên trình duyệt
  2. 2Browse qua các HTTP methods: GET, POST, PUT, DELETE, etc.
  3. 3Sử dụng search box để tìm method cụ thể
  4. 4Đọc description và use cases cho mỗi method
  5. 5Xem properties: idempotent, safe, cacheable
  6. 6Check ví dụ HTTP request code
  7. 7Xem status codes phổ biến cho method đó
  8. 8Click Copy để copy example code
  9. 9Paste vào API client hoặc documentation
  10. 10Bookmark trang để tra cứu sau này

Idempotent vs Safe Methods

Safe methods (GET, HEAD, OPTIONS) không thay đổi server state, chỉ retrieve data. Idempotent methods (GET, PUT, DELETE, HEAD, OPTIONS) cho kết quả giống nhau dù gọi 1 lần hay nhiều lần. POST và PATCH không idempotent vì mỗi request có thể tạo resource mới hoặc thay đổi state khác nhau. Hiểu properties này quan trọng cho retry logic và API design.

RESTful API Best Practices

Khi design RESTful APIs: dùng GET cho reading, POST cho creating, PUT cho full updates, PATCH cho partial updates, DELETE cho removing. Use proper status codes: 2xx cho success, 4xx cho client errors, 5xx cho server errors. Implement proper error handling, versioning, authentication, và rate limiting. Follow naming conventions: plural nouns cho resources (/users, /posts), avoid verbs trong URLs.

CORS và Preflight Requests

Browser gửi OPTIONS preflight request trước actual request khi: cross-origin request, custom headers, hoặc methods khác GET/POST. Server phải respond với proper CORS headers: Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers. Preflight responses có thể được cached với Access-Control-Max-Age. Hiểu CORS quan trọng cho frontend-backend communication.

Câu hỏi thường gặp (FAQ)

GET và POST khác nhau như thế nào?

GET dùng để retrieve data, parameters trong URL, idempotent và safe, có thể cached. POST dùng để create resources, data trong request body, không idempotent, không cached. GET cho reading, POST cho creating/submitting data.

Khi nào dùng PUT vs PATCH?

PUT replace toàn bộ resource, phải gửi full data. PATCH chỉ update một phần, gửi fields cần thay đổi. Ví dụ: PUT /users/1 với full user object, PATCH /users/1 chỉ với {email: 'new@email.com'}. PATCH tiết kiệm bandwidth hơn.

Idempotent có nghĩa là gì?

Idempotent method cho kết quả giống nhau dù gọi 1 lần hay nhiều lần. GET, PUT, DELETE, HEAD, OPTIONS là idempotent. POST không idempotent vì mỗi request có thể tạo resource mới. Idempotency quan trọng cho retry logic và reliability.

HEAD method dùng để làm gì?

HEAD giống GET nhưng chỉ trả về headers, không có body. Dùng để check resource existence, get metadata (size, last-modified), hoặc validate cache mà không download full content. Tiết kiệm bandwidth khi chỉ cần metadata.

OPTIONS method có vai trò gì?

OPTIONS dùng để discover allowed methods cho resource và handle CORS preflight requests. Browser tự động gửi OPTIONS trước cross-origin requests. Server respond với Access-Control-Allow-Methods header listing allowed methods.

Có thể cache POST requests không?

Theo HTTP spec, POST responses không cacheable by default trừ khi có explicit Cache-Control hoặc Expires headers. Trong thực tế, POST thường không được cached vì tạo side effects. GET và HEAD là cacheable by default.

DELETE có idempotent không?

Có, DELETE là idempotent. Gọi DELETE /users/1 nhiều lần cho kết quả giống nhau: resource bị xóa. Lần đầu return 200/204, lần sau có thể return 404, nhưng end state giống nhau (resource không tồn tại).

Status code nào phù hợp cho POST success?

201 Created khi tạo resource mới (nên include Location header với URL của resource). 200 OK khi POST không tạo resource mới nhưng process thành công. 204 No Content khi success nhưng không có data trả về.

Từ khóa liên quan

http methods explainedrest api methodsget post put deletehttp verbsrestful api designhttp request methodsapi methods guidehttp protocol methodspatch vs putidempotent methods

Hợp tác ngay với Tấn Phát Digital

Chúng tôi không chỉ thiết kế website, mà còn giúp doanh nghiệp xây dựng thương hiệu số mạnh mẽ. Cung cấp dịch vụ thiết kế website trọn gói từ thiết kế đến tối ưu SEO. Hãy liên hệ ngay với Tấn Phát Digital để cùng tạo nên những giải pháp công nghệ đột phá, hiệu quả và bền vững cho doanh nghiệp của bạn tại Hồ Chí Minh.

Công cụ Developer Tools liên quan

Zalo
Facebook
Tấn Phát Digital
Zalo
Facebook