# Referensi Cepat Kode Status HTTP

*Kode status, header, dan pola respons umum*

> Source: HTTP/1.1 Specification (RFC 9110) · MIT

## Informasional 1xx

### Kode 1xx

| Command | Description |
|---------|-------------|
| `100` | Continue — server menerima header, client harus kirim body |
| `101` | Switching Protocols — upgrade ke WebSocket atau HTTP/2 |
| `102` | Processing — server menerima request, masih memproses (WebDAV) |
| `103` | Early Hints — preload resource sebelum respons akhir |

### Catatan Penggunaan

```
# 100 Continue: client sends Expect header, waits for 100
curl -H "Expect: 100-continue" -d @large.json URL
# 101: upgrade to WebSocket
Connection: Upgrade / Upgrade: websocket
```

## Sukses 2xx

### Kode 2xx

| Command | Description |
|---------|-------------|
| `200` | OK — respons sukses standar |
| `201` | Created — resource berhasil dibuat (POST/PUT) |
| `202` | Accepted — request diterima, diproses secara async |
| `203` | Non-Authoritative Info — ditransformasi oleh proxy |
| `204` | No Content — sukses tanpa body respons (DELETE) |
| `205` | Reset Content — sukses, client harus reset form |
| `206` | Partial Content — request range terpenuhi |
| `207` | Multi-Status — beberapa kode status (WebDAV) |

### Penggunaan REST API

| Command | Description |
|---------|-------------|
| `GET → 200` | Kembalikan resource dengan body |
| `POST → 201` | Resource dibuat, sertakan header Location |
| `PUT → 200/204` | Resource diperbarui (dengan/tanpa body) |
| `DELETE → 204` | Dihapus, tidak ada body yang dikembalikan |
| `PATCH → 200` | Pembaruan parsial, kembalikan resource yang diubah |

## Pengalihan 3xx

### Kode 3xx

| Command | Description |
|---------|-------------|
| `300` | Multiple Choices — beberapa representasi tersedia |
| `301` | Moved Permanently — resource dipindah, perbarui bookmark |
| `302` | Found — redirect sementara (sering disalahgunakan sebagai 303) |
| `303` | See Other — redirect dengan GET setelah POST |
| `304` | Not Modified — gunakan versi cache (ETag/If-Modified) |
| `307` | Temporary Redirect — metode sama, lokasi sementara |
| `308` | Permanent Redirect — metode sama, lokasi permanen |

### Perilaku Redirect

| Command | Description |
|---------|-------------|
| `301/308` | Permanen — mesin pencari memperbarui indeks |
| `302/307` | Sementara — URL asli tetap kanonik |
| `301/302` | Mungkin mengubah metode ke GET saat redirect |
| `307/308` | Harus mempertahankan metode HTTP asli |

## Error Client 4xx

### Error Client Umum

| Command | Description |
|---------|-------------|
| `400` | Bad Request — sintaks salah atau parameter tidak valid |
| `401` | Unauthorized — autentikasi diperlukan atau gagal |
| `403` | Forbidden — terautentikasi tetapi tidak diizinkan |
| `404` | Not Found — resource tidak ada |
| `405` | Method Not Allowed — metode HTTP tidak didukung |
| `406` | Not Acceptable — tidak bisa memenuhi header Accept |
| `408` | Request Timeout — client terlalu lambat mengirim request |
| `409` | Conflict — request bertentangan dengan state saat ini |

### Error Client Lainnya

| Command | Description |
|---------|-------------|
| `410` | Gone — resource dihapus permanen (bukan hanya hilang) |
| `411` | Length Required — header Content-Length tidak ada |
| `412` | Precondition Failed — If-Match/If-Unmodified gagal |
| `413` | Content Too Large — body request melebihi batas |
| `414` | URI Too Long — URL melebihi batas server |
| `415` | Unsupported Media Type — Content-Type tidak diterima |
| `422` | Unprocessable Content — sintaks valid, kesalahan semantik |
| `429` | Too Many Requests — batas rate terlampaui |

## Error Server 5xx

### Kode 5xx

| Command | Description |
|---------|-------------|
| `500` | Internal Server Error — exception tidak tertangani di server |
| `501` | Not Implemented — server tidak mendukung metode tersebut |
| `502` | Bad Gateway — server upstream mengirim respons tidak valid |
| `503` | Service Unavailable — kelebihan beban atau dalam pemeliharaan |
| `504` | Gateway Timeout — server upstream tidak merespons tepat waktu |
| `505` | HTTP Version Not Supported — versi tidak ditangani |
| `507` | Insufficient Storage — server tidak bisa menyimpan request (WebDAV) |
| `511` | Network Auth Required — perlu login captive portal |

### Strategi Retry

| Command | Description |
|---------|-------------|
| `500` | Retry dengan backoff; mungkin sementara |
| `502/504` | Retry — masalah upstream mungkin teratasi |
| `503` | Periksa header Retry-After sebelum retry |
| `501/505` | Jangan retry — perbaiki request client |

## Kode Umum

### Kode Paling Sering Digunakan

| Command | Description |
|---------|-------------|
| `200` | OK — semuanya berjalan |
| `201` | Created — resource baru dibuat |
| `204` | No Content — sukses, body kosong |
| `301` | Moved Permanently — perbarui URL |
| `304` | Not Modified — gunakan cache |
| `400` | Bad Request — perbaiki request Anda |
| `401` | Unauthorized — login terlebih dahulu |
| `403` | Forbidden — izin tidak cukup |
| `404` | Not Found — URL salah atau dihapus |
| `422` | Unprocessable — kesalahan validasi |
| `429` | Too Many Requests — perlambat |
| `500` | Server Error — bukan kesalahan Anda |
| `502` | Bad Gateway — kegagalan proxy/upstream |
| `503` | Unavailable — coba lagi nanti |

## Referensi Header

### Header Request

| Command | Description |
|---------|-------------|
| `Accept` | Tipe media respons yang diinginkan (mis. application/json) |
| `Authorization` | Kredensial (Bearer token, Basic base64) |
| `Content-Type` | Tipe media dari body request |
| `If-None-Match` | Kondisional: ETag untuk validasi cache |
| `If-Modified-Since` | Kondisional: tanggal untuk validasi cache |
| `Cache-Control` | Arahan caching (no-cache, max-age) |
| `User-Agent` | String identifikasi client |

### Header Response

| Command | Description |
|---------|-------------|
| `Content-Type` | Tipe media dari body respons |
| `Location` | URL tujuan redirect atau resource yang dibuat |
| `ETag` | Tag entitas untuk validasi cache |
| `Cache-Control` | Arahan caching (max-age, no-store) |
| `Retry-After` | Waktu tunggu sebelum retry (429/503) |
| `WWW-Authenticate` | Skema autentikasi yang diperlukan (dikirim dengan 401) |
| `Set-Cookie` | Set cookie di client |

## Pola Umum

### Alur Caching

```
# First request — server returns ETag
GET /api/data → 200, ETag: "abc123"
# Subsequent request — conditional
GET /api/data, If-None-Match: "abc123"
→ 304 Not Modified (use cache)
```

### Alur Autentikasi

```
# Unauthenticated request
GET /api/secret → 401, WWW-Authenticate: Bearer
# With token
GET /api/secret, Authorization: Bearer <token>
→ 200 OK
```

### Pembatasan Rate

```
# Rate limited response
429 Too Many Requests
Retry-After: 60
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 1700000000
```

### Negosiasi Konten

```
# Client prefers JSON, accepts XML
Accept: application/json, application/xml;q=0.9
# Server can't satisfy → 406 Not Acceptable
# Server returns best match → 200 + Content-Type
```
