HTTP Status Codes
HTTP Status Codes
Reference สำหรับ HTTP status codes ทั้ง 5 หมวด (1xx-5xx) พร้อมแนวทางใช้งานจริงตอนออกแบบ REST API และตอนจัดการ error ฝั่ง frontend
Why Status Codes Exist
HTTP status code คือ machine-readable, standardized signal ที่บอกผลลัพธ์ของ request โดยไม่ต้อง parse response body ก่อน — client (browser, fetch, axios, load balancer, monitoring tool) เช็คแค่ตัวเลข 3 หลักก็รู้ทันทีว่า request สำเร็จ, ต้อง redirect, client ทำผิด, หรือ server พัง
ถ้าไม่มี status code มาตรฐาน แต่ละ server จะสื่อผลลัพธ์ผ่านข้อความใน body เอง เช่น "success", "done", "ok", "error" — ซึ่งไม่สอดคล้องกันข้าม service, ข้าม team, ข้าม framework ทำให้ client ต้อง parse string เดาความหมายแทนที่จะเช็ค convention ที่ตกลงร่วมกันทั้งอุตสาหกรรม การมี status code มาตรฐานยังทำให้ tooling ทั่วไป (caching layer, retry logic, monitoring/alerting, API gateway) ทำงานได้ถูกต้องโดยไม่ต้องรู้ business logic ของแต่ละ endpoint
1xx Informational
| Code | Name | Meaning | When to use |
|---|---|---|---|
| 100 | Continue | Server ได้รับ request headers แล้ว client ส่ง body ต่อได้ | Server ตอบอัตโนมัติเมื่อ client ส่ง header Expect: 100-continue ก่อน upload body ก้อนใหญ่ |
| 101 | Switching Protocols | Server ยอมรับ request เปลี่ยน protocol ตาม header Upgrade |
WebSocket handshake (Upgrade: websocket), HTTP/2 upgrade |
| 102 | Processing | Server รับ request แล้วและกำลังประมวลผล แต่ยังไม่มี response พร้อม | WebDAV เท่านั้น — ใช้กันไม่ได้แล้วป้องกัน client timeout ระหว่างรอ operation ที่ใช้เวลานาน |