HTTP DOCS

HTTP-METHODS

GET
Description: Mengambil representasi resource. Hanya mengambil data, tidak mengubah state. Aman dan idempoten.
Example:
GET /api/users HTTP/1.1
Host: example.com
Output: 200 OK [{...}, {...}]
POST
Description: Membuat resource baru. Tidak aman, tidak idempoten.
Example:
POST /api/users HTTP/1.1
Content-Type: application/json

{"name":"Ridho"}
Output: 201 Created Location: /api/users/123
PUT
Description: Mengganti seluruh resource atau membuat jika belum ada. Idempoten.
Example:
PUT /api/users/123 HTTP/1.1
Content-Type: application/json

{"name":"Ridho","age":30}
Output: 200 OK (atau 204 No Content)
PATCH
Description: Memperbarui sebagian resource. Tidak idempoten (bisa idempoten tergantung implementasi).
Example:
PATCH /api/users/123 HTTP/1.1
Content-Type: application/json

{"age":31}
Output: 200 OK
DELETE
Description: Menghapus resource. Idempoten.
Example:
DELETE /api/users/123 HTTP/1.1
Output: 204 No Content
HEAD
Description: Sama seperti GET tetapi hanya mengembalikan header, tanpa body. Aman dan idempoten.
Example:
HEAD /api/users HTTP/1.1
Output: 200 OK (hanya header)
OPTIONS
Description: Mendeskripsikan opsi komunikasi untuk resource target. Digunakan untuk CORS preflight.
Example:
OPTIONS /api/users HTTP/1.1
Output: 204 No Content Allow: GET, POST, OPTIONS
TRACE
Description: Melakukan loop-back test di sepanjang jalur ke resource. Jarang digunakan, sering dinonaktifkan.
Example:
TRACE /api/users HTTP/1.1
Output: 200 OK (echo request)
CONNECT
Description: Membuat terowongan ke server (biasanya untuk HTTPS melalui proxy).
Example:
CONNECT example.com:443 HTTP/1.1
Output: 200 Connection Established

STATUS-CODES

1xx – Informational
Description: Menandakan permintaan diterima dan dilanjutkan.
Example:
100 Continue, 101 Switching Protocols
Output: Respons sementara
100 Continue
Description: Klien harus melanjutkan permintaan atau mengabaikan respons ini jika sudah selesai.
Example:
HTTP/1.1 100 Continue
Output: Klien dapat mengirim body
101 Switching Protocols
Description: Server setuju untuk mengganti protokol sesuai permintaan klien (misal WebSocket).
Example:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Output: Koneksi berubah protokol
2xx – Success
Description: Permintaan berhasil diterima, dipahami, dan diterima.
Example:
200 OK, 201 Created, 204 No Content
Output: Berhasil
200 OK
Description: Respons sukses standar untuk GET, PUT, atau PATCH.
Example:
HTTP/1.1 200 OK
Output: Body respons
201 Created
Description: Resource baru berhasil dibuat (biasanya dari POST).
Example:
HTTP/1.1 201 Created
Location: /users/123
Output: Lokasi resource baru
204 No Content
Description: Sukses tetapi tidak ada konten di body (umum untuk DELETE).
Example:
HTTP/1.1 204 No Content
Output: Body kosong
3xx – Redirection
Description: Dibutuhkan aksi tambahan dari klien untuk menyelesaikan permintaan.
Example:
301 Moved Permanently, 302 Found, 304 Not Modified
Output: Redirect atau cache
301 Moved Permanently
Description: Resource telah dipindahkan secara permanen ke URL baru. Klien harus menggunakan URL baru di masa depan.
Example:
HTTP/1.1 301 Moved Permanently
Location: https://new.example.com/page
Output: Redirect permanen
302 Found
Description: Redirect sementara. Klien tetap menggunakan URL asli untuk permintaan berikutnya.
Example:
HTTP/1.1 302 Found
Location: /login
Output: Redirect sementara
304 Not Modified
Description: Resource tidak berubah sejak versi yang diminta. Klien dapat menggunakan versi cache.
Example:
HTTP/1.1 304 Not Modified
Output: Gunakan cache
307 Temporary Redirect / 308 Permanent Redirect
Description: Mirip 302/301 tetapi tidak mengubah metode HTTP (POST tetap POST).
Example:
HTTP/1.1 307 Temporary Redirect
Location: /new-url
Output: Redirect dengan metode dipertahankan
4xx – Client Error
Description: Kesalahan di sisi klien.
Example:
400 Bad Request, 401 Unauthorized, 404 Not Found
Output: Error klien
400 Bad Request
Description: Server tidak memahami permintaan karena sintaks yang salah.
Example:
HTTP/1.1 400 Bad Request
Output: Body kosong atau format salah
401 Unauthorized
Description: Autentikasi diperlukan (belum login atau token salah).
Example:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer
Output: Harus menyertakan kredensial
403 Forbidden
Description: Server memahami permintaan tetapi menolak mengotorisasi (akses ditolak).
Example:
HTTP/1.1 403 Forbidden
Output: Tidak ada izin
404 Not Found
Description: Server tidak dapat menemukan resource yang diminta.
Example:
HTTP/1.1 404 Not Found
Output: Resource tidak ada
429 Too Many Requests
Description: Pengguna telah mengirim terlalu banyak permintaan dalam waktu tertentu (rate limiting).
Example:
HTTP/1.1 429 Too Many Requests
Retry-After: 60
Output: Coba lagi nanti
5xx – Server Error
Description: Kesalahan di sisi server.
Example:
500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable
Output: Error server
500 Internal Server Error
Description: Kesalahan generik server yang tidak terduga.
Example:
HTTP/1.1 500 Internal Server Error
Output: Body error
502 Bad Gateway
Description: Server bertindak sebagai gateway/proxy dan menerima respons tidak valid dari upstream.
Example:
HTTP/1.1 502 Bad Gateway
Output: Upstream error
503 Service Unavailable
Description: Server sementara tidak tersedia (kelebihan beban atau maintenance).
Example:
HTTP/1.1 503 Service Unavailable
Retry-After: 120
Output: Coba lagi nanti

GENERAL-HEADERS

Cache-Control
Description: Mengontrol caching di browser dan CDN. Directives: max-age, no-cache, no-store, must-revalidate.
Example:
Cache-Control: public, max-age=31536000, immutable
Output: Di-cache 1 tahun
Connection
Description: Mengontrol apakah koneksi jaringan tetap terbuka setelah transaksi selesai.
Example:
Connection: keep-alive
Output: Koneksi persisten
Date
Description: Tanggal dan waktu saat pesan dibuat.
Example:
Date: Mon, 28 Jun 2025 12:00:00 GMT
Output: Timestamp
Transfer-Encoding
Description: Bentuk encoding yang digunakan untuk mengirim body ke klien (chunked).
Example:
Transfer-Encoding: chunked
Output: Body dikirim bertahap

REQUEST-HEADERS

Accept
Description: Memberi tahu server tipe konten yang dapat diterima klien (MIME type).
Example:
Accept: application/json
Output: Prioritas JSON
Authorization
Description: Kredensial autentikasi (Basic, Bearer token).
Example:
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
Output: Token JWT
Content-Type
Description: Tipe media dari body permintaan.
Example:
Content-Type: application/json
Output: Body dalam format JSON
Cookie
Description: Mengirim cookie yang tersimpan ke server.
Example:
Cookie: session_id=abc123; theme=dark
Output: Data cookie
Host
Description: Nama domain server (wajib di HTTP/1.1).
Example:
Host: example.com
Output: Server target
User-Agent
Description: Informasi tentang browser atau aplikasi klien.
Example:
User-Agent: Mozilla/5.0 ...
Output: Identitas klien

RESPONSE-HEADERS

Access-Control-Allow-Origin
Description: Mengizinkan asal lintas domain untuk mengakses resource (CORS).
Example:
Access-Control-Allow-Origin: *
Output: Semua origin diizinkan
ETag
Description: Identifier versi resource untuk validasi cache.
Example:
ETag: "33a64df551..."
Output: Digunakan dengan If-None-Match
Location
Description: URL untuk redirect atau resource yang baru dibuat.
Example:
Location: https://example.com/new-page
Output: URL tujuan
Set-Cookie
Description: Mengirim cookie ke klien untuk disimpan.
Example:
Set-Cookie: session=abc; Secure; HttpOnly; SameSite=Lax
Output: Cookie diset
WWW-Authenticate
Description: Menunjukkan metode autentikasi yang diperlukan (bersama 401).
Example:
WWW-Authenticate: Bearer realm="API"
Output: Metode autentikasi

CORS

Same-Origin Policy
Description: Batasan browser yang mencegah halaman mengakses resource dari origin berbeda (protocol, domain, port).
Example:
fetch('https://other.com/api') → diblokir
Output: Diblokir oleh browser
Simple Request
Description: Request yang tidak memicu preflight: GET, POST, HEAD dengan content-type tertentu.
Example:
POST dengan Content-Type: application/x-www-form-urlencoded
Output: Langsung dikirim
Preflight Request (OPTIONS)
Description: Request pendahuluan untuk memeriksa izin CORS sebelum request sebenarnya.
Example:
OPTIONS /api HTTP/1.1
Access-Control-Request-Method: DELETE
Output: Respons berisi header izin
Access-Control-Max-Age
Description: Berapa lama hasil preflight dapat di-cache (dalam detik).
Example:
Access-Control-Max-Age: 86400
Output: Cache 24 jam

CACHING-MECHANISMS

ETag / If-None-Match
Description: Validasi cache: server mengirim ETag, klien mengirim If-None-Match. Jika cocok, server balas 304.
Example:
Request: If-None-Match: "abc"
Response: 304 Not Modified
Output: Menghemat bandwidth
Last-Modified / If-Modified-Since
Description: Validasi berdasarkan waktu modifikasi terakhir.
Example:
Request: If-Modified-Since: Mon, 28 Jun 2025 10:00:00 GMT
Response: 304 Not Modified
Output: Cache valid
Cache-Control: no-store
Description: Tidak menyimpan cache sama sekali. Setiap request harus ke server.
Example:
Cache-Control: no-store
Output: Cache dinonaktifkan
Cache-Control: no-cache
Description: Boleh cache, tapi harus divalidasi ulang ke server sebelum digunakan.
Example:
Cache-Control: no-cache
Output: Validasi setiap kali

AUTHENTICATION-SCHEMES

Basic Authentication
Description: Mengirim username:password dalam header Authorization, diencode base64.
Example:
Authorization: Basic dXNlcjpwYXNz
Output: Kredensial base64 (tidak aman tanpa HTTPS)
Bearer Token
Description: Mengirim token (JWT, OAuth2) di header Authorization.
Example:
Authorization: Bearer eyJhbGciOi...
Output: Token terverifikasi
Digest Authentication
Description: Lebih aman dari Basic, tidak mengirim password secara langsung.
Example:
Authorization: Digest username="user", realm="API", nonce="..."
Output: Hash password
API Key
Description: Mengirim kunci API di header kustom (X-API-Key) atau query parameter.
Example:
X-API-Key: sk_live_abc123
Output: API key terautentikasi

CONTENT-NEGOTIATION

Accept Header
Description: Klien memberitahu server tipe konten yang diinginkan (application/json, text/html).
Example:
Accept: application/json, text/html;q=0.9
Output: JSON diprioritaskan
Accept-Language
Description: Klien memberitahu server bahasa yang diinginkan.
Example:
Accept-Language: id-ID, en;q=0.8
Output: Bahasa Indonesia diprioritaskan
Accept-Encoding
Description: Klien memberitahu server encoding konten yang didukung (gzip, deflate, br).
Example:
Accept-Encoding: gzip, deflate, br
Output: Brotli dipilih jika didukung

HTTP-VERSIONS

HTTP/1.0
Description: Setiap request membuka koneksi baru. Header Host tidak wajib.
Example:
GET /page.html HTTP/1.0
Output: Koneksi langsung ditutup
HTTP/1.1
Description: Koneksi persisten (keep-alive), header Host wajib, chunked transfer.
Example:
GET /api HTTP/1.1
Host: example.com
Connection: keep-alive
Output: Koneksi tetap terbuka
HTTP/2
Description: Binary protocol, multiplexing (banyak request dalam satu koneksi), header compression, server push.
Example:
Semua request ke satu domain menggunakan satu koneksi TCP.
Output: Lebih cepat, lebih efisien
HTTP/3
Description: Berbasis QUIC (UDP), tanpa head-of-line blocking, koneksi lebih cepat di jaringan lossy.
Example:
Alt-Svc: h3=":443"
Output: Koneksi QUIC

BEST-PRACTICES

Gunakan HTTPS
Description: Enkripsi semua komunikasi HTTP dengan TLS.
Example:
Server mendukung HTTPS dengan redirect HTTP ke HTTPS.
Output: Data aman
Gunakan HTTP/2 atau HTTP/3
Description: Manfaatkan multiplexing untuk loading halaman lebih cepat.
Example:
Aktifkan HTTP/2 di web server (Nginx, Apache).
Output: Performa meningkat
Gunakan metode HTTP dengan benar
Description: GET untuk mengambil, POST untuk membuat, PUT untuk mengganti, DELETE untuk menghapus.
Example:
Hindari GET untuk operasi yang mengubah data.
Output: RESTful API
Rate Limiting dengan 429
Description: Batasi jumlah permintaan untuk mencegah abuse.
Example:
return 429 Too Many Requests dengan header Retry-After.
Output: Melindungi server