ရိုးရိုးလေး ရှင်းပြမယ်
HTTP method တွေကို ရိုးရိုးလေး ပြောရရင်
HTTP method တွေက API ရဲ့ ကြိယာ (verb) တွေပါ။ URL က ဘာအကြောင်း ပြောနေလဲ (note တစ်ခု၊ user စာရင်း) ကို ပြောပြီး method က အဲဒီအပေါ် ဘာလုပ်ချင်လဲ — ဖတ်မလား၊ ဖန်တီးမလား၊ အစားထိုးမလား၊ ပြင်မလား၊ ဖျက်မလား — ကို ပြောပါတယ်။ URL တူပေမဲ့ method မတူရင် လုပ်ဆောင်ချက် မတူပါဘူး။
စာကြည့်တိုက် counter တစ်ခုနဲ့ တူပါတယ်။ စာအုပ်ရဲ့ နာမည် (URL) က အတူတူပဲ၊ ဒါပေမဲ့ 'ပြပါ'၊ 'စာအုပ်အသစ် ထည့်ပါ'၊ 'ဒီစာအုပ်ကို လဲပေးပါ'၊ 'အဖုံးလေး ပြင်ပါ'၊ 'စာရင်းကနေ ဖယ်ပါ' ဆိုတာတွေက တောင်းဆိုချက် မတူကြပါဘူး။ ပိုအရေးကြီးတာက တချို့ တောင်းဆိုချက်တွေကို နှစ်ခါပြောလည်း ရလဒ် တူပြီး တချို့ကတော့ မတူတာပါ၊ အဲဒါက ဒီ lesson ရဲ့ အဓိကအချက်ပါ။
Problem → Why → Solution
ပြဿနာ: GET link တစ်ခုက order ကို ဖျက်ပစ်တယ်
Email ထဲက 'Order ပယ်ဖျက်မယ်' ခလုတ်ကို /orders/7/cancel ဆိုတဲ့ ရိုးရိုး link အဖြစ် ရေးထားတယ် ဆိုပါစို့။ အလုပ်လုပ်ပါတယ်၊ ပြီးတော့ တစ်ရက်မှာ customer တွေ ဘာမှ မနှိပ်ဘဲ order တွေ ပျောက်သွားပါတယ်။ Email security scanner၊ chat app ရဲ့ link preview bot နဲ့ link တွေကို ကြိုဖွင့်တတ်တဲ့ browser တို့က အဲဒီ URL ကို GET နဲ့ ဖွင့်ကြည့်ကြတာမို့ အားလုံး cancel ဖြစ်သွားတာပါ။
// BROKEN: a state-changing action hidden behind GET (Express-style handler)
app.get("/orders/:id/cancel", (req, res) => {
orders.cancel(req.params.id);
res.send("Order cancelled");
});
// Anything that merely FETCHES this URL cancels the order:
// <a href="/orders/7/cancel">Cancel</a> in an email, a link-preview bot,
// a security scanner, or a browser that prefetches links.GET ကို safe လို့ လူတိုင်း ယူဆထားတယ်
HTTP မှာ GET ကို data ဖတ်ဖို့ပဲ သတ်မှတ်ထားပြီး cache, crawler, prefetcher နဲ့ proxy တွေက ဘာမှ မဖြစ်ဘူးလို့ ယူဆပြီး အလိုလို ခေါ်ကြပါတယ်။ အဲဒီ ယူဆချက်ကို ချိုးဖောက်ရင် ကိုယ့် API ကို မသိတဲ့ ကိရိယာတွေက အန္တရာယ်ဖြစ်စေပါတယ်။ ပြောင်းလဲမှုကို POST, PUT, PATCH ဒါမှမဟုတ် DELETE ထဲမှာပဲ ထားပြီး ဘယ်လိုပြန်ခေါ်ရင် ဘာဖြစ်မလဲကို method ကနေပဲ ကြိုသိနိုင်အောင် လုပ်ပါ။
Core concept: method က ဘာလုပ်မလဲ၊ ပြန်ခေါ်ရင် ဘာဖြစ်မလဲ ပြောတယ်
Method တစ်ခုချင်းစီမှာ အချက်နှစ်ခု ပါပါတယ်။ Safe ဆိုတာ server ရဲ့ state ကို မပြောင်းဘူး၊ ဖတ်ရုံသက်သက်ဆိုတဲ့ အဓိပ္ပာယ်ပါ (GET, HEAD, OPTIONS)။ Idempotent ဆိုတာ တူညီတဲ့ request ကို တစ်ကြိမ်ပို့တာနဲ့ အကြိမ်များစွာ ပို့တာ server ရဲ့ နောက်ဆုံး state က အတူတူ ဖြစ်တယ်ဆိုတဲ့ အဓိပ္ပာယ်ပါ (GET, HEAD, OPTIONS, PUT, DELETE)။ Idempotent ဆိုတာ response တူတယ်လို့ မဆိုလိုပါဘူး: DELETE ကို ဒုတိယအကြိမ် ပို့ရင် 204 အစား 404 ရနိုင်ပေမဲ့ server ပေါ်မှာ ပျောက်သွားတာက အတူတူပါပဲ။
POST က အသုံးအများဆုံးပါ: collection ထဲကို resource အသစ် ဖန်တီးတာ ဒါမှမဟုတ် 'ဒီအလုပ်ကို run ပါ' လို action တစ်ခု လုပ်ခိုင်းတာပါ။ Server က id ကို ရွေးပေးလို့ အကြိမ်တိုင်း ရလဒ်အသစ် ရပါတယ်၊ ဒါကြောင့် POST က safe လည်းမဟုတ် idempotent လည်း မဟုတ်ပါဘူး။ လုပ်ငန်းစဉ်ကောင်းကတော့ 201 Created နဲ့ အသစ်ရဲ့ URL ကို Location header ထဲ ပြန်ပေးတာပါ။ Timeout ကြောင့် ပြန်ပို့ရင် order နှစ်ခု ဖြစ်သွားနိုင်လို့ payment လို API တွေက Idempotency-Key header ကို သုံးကြပါတယ်။
PUT က သိပြီးသား URL မှာ resource တစ်ခုလုံးကို အစားထိုးတာပါ: ပို့တဲ့ body ကသာ resource ရဲ့ အသစ်ပုံစံ ဖြစ်သွားလို့ မပါတဲ့ field တွေက ပျောက်သွားနိုင်ပါတယ် (မရှိသေးရင် 201 နဲ့ ဖန်တီးပေးလည်း ရပါတယ်)။ PATCH ကတော့ ပို့လိုက်တဲ့ field တွေကိုပဲ ပြောင်းပါတယ်။ HTTP spec မှာ PATCH က idempotent လို့ အာမမခံပါဘူး: {"done": true} လို set လုပ်တာ ပြန်လုပ်လည်း အတူတူပေမဲ့ 'တစ်ခု တိုး' လို့ ပို့ရင် အကြိမ်တိုင်း ရလဒ် ပြောင်းပါတယ်။ DELETE က resource ကို ဖယ်ပါတယ်၊ ပုံမှန်အားဖြင့် 204 No Content ပါ။
| Method | သာမန်အသုံးပြုပုံ | Safe / idempotent |
|---|---|---|
| GET | Resource ကို ဖတ် | Safe၊ idempotent |
| HEAD | GET လိုပဲ header သက်သက် | Safe၊ idempotent |
| POST | ဖန်တီး ဒါမှမဟုတ် action လုပ် | နှစ်ခုလုံး မဟုတ် |
| PUT | Resource တစ်ခုလုံးကို အစားထိုး | Idempotent |
| PATCH | Field အချို့ကိုပဲ ပြင် | အာမမခံ |
| DELETE | Resource ကို ဖယ် | Idempotent |
| OPTIONS | ဘာတွေ ခွင့်ပြုလဲ မေး | Safe၊ idempotent |
- Resource ရွေး: /notes/1 လို URL
- Verb ရွေး: ဘာဖြစ်စေချင်လဲ
- Safe လား: GET, HEAD, OPTIONS ပဲ
- ပြန်ခေါ်လို့ရလား: PUT, DELETE ရ၊ POST မရ
- မထောက်ပံ့တဲ့ verb: 405 + Allow
Retry လုပ်ဖို့ စဉ်းစားတိုင်း 'ဒီ method ကို နှစ်ခါပို့ရင် ဘာဖြစ်မလဲ' လို့ အရင်မေးပါ။
အထွေထွေ ပုံစံ
POST /things create a thing -> 201 Created + Location
GET /things list things -> 200 OK
GET /things/{id} read one -> 200 OK, or 404
PUT /things/{id} replace the whole one -> 200 OK (201 if it was new)
PATCH /things/{id} change some fields -> 200 OK
DELETE /things/{id} remove it -> 204 No Content
other any method not allowed here -> 405 + Allow headerCollection URL မှာ ဖန်တီးနဲ့ စာရင်းထုတ်၊ item URL မှာ ဖတ်၊ အစားထိုး၊ ပြင်၊ ဖျက်လုပ်ပါတယ်။ ထောက်ပံ့မထားတဲ့ ပေါင်းစပ်မှုတိုင်းကို 405 နဲ့ ပြန်ပါ။
Worked example: notes API နဲ့ method တစ်ခုချင်းစီကို စမ်းခြင်း
အောက်က script က memory ထဲမှာ note တွေ သိမ်းတဲ့ server တစ်ခု စတင်ပြီး method အမျိုးမျိုးကို ပို့ပါတယ်။ Output ကို ဒီလို ဖတ်ပါ: POST နှစ်ခါ ပို့ရင် id 1 နဲ့ 2 နှစ်ခု ရပါတယ်။ PUT ကို တူညီတဲ့ body နဲ့ နှစ်ခါ ပို့ရင် အဖြေ တူပါတယ်။ ပြီးတာနဲ့ done မပါတဲ့ PUT တစ်ခု ပို့ကြည့်ရင် done က false ဖြစ်သွားပါတယ်၊ PUT က အစားထိုးတာမို့ပါ။ PATCH ကတော့ done တစ်ခုပဲ ပြောင်းပါတယ်။
ဆက်ပြီး DELETE ကို နှစ်ခါပို့ရင် ပထမက 204၊ ဒုတိယက 404 ပါ၊ ဒါပေမဲ့ note 2 မရှိတာကတော့ နှစ်ခုလုံးမှာ တူပါတယ်။ Collection ကို DELETE လုပ်ရင် 405 နဲ့ Allow header ပြန်ပြီး OPTIONS ကတော့ body မပါဘဲ Allow ကိုပဲ ပြန်ပါတယ်။
import http from "node:http";
const notes = new Map(); // id -> { id, title, done }
let nextId = 1;
const server = http.createServer(async (req, res) => {
const send = (status, body, headers = {}) => {
if (body === undefined) {
res.writeHead(status, headers); // 204 has no body, so no Content-Type either
return res.end();
}
res.writeHead(status, { "Content-Type": "application/json", ...headers });
res.end(JSON.stringify(body));
};
let raw = "";
for await (const chunk of req) raw += chunk;
const body = raw ? JSON.parse(raw) : {};
const [, collection, rawId] = req.url.split("/"); // "/notes/1" -> ["", "notes", "1"]
const id = rawId === undefined ? null : Number(rawId);
if (collection !== "notes" || Number.isNaN(id)) {
return send(404, { error: "not_found" });
}
const allow = id === null
? ["GET", "POST", "OPTIONS"]
: ["GET", "PUT", "PATCH", "DELETE", "OPTIONS"];
if (!allow.includes(req.method)) {
return send(405, { error: "method_not_allowed" }, { Allow: allow.join(", ") });
}
if (req.method === "OPTIONS") return send(204, undefined, { Allow: allow.join(", ") });
if (id === null) {
if (req.method === "GET") return send(200, [...notes.values()]);
// POST: the server, not the client, picks the new id
const note = { id: nextId++, title: body.title, done: false };
notes.set(note.id, note);
return send(201, note, { Location: `/notes/${note.id}` });
}
const existing = notes.get(id);
if (req.method === "PUT") { // replaces the whole resource, creating it if needed
notes.set(id, { id, title: body.title, done: body.done ?? false });
return send(existing ? 200 : 201, notes.get(id));
}
if (!existing) return send(404, { error: "not_found" });
if (req.method === "GET") return send(200, existing);
if (req.method === "PATCH") { // changes only the fields that were sent
Object.assign(existing, body, { id });
return send(200, existing);
}
notes.delete(id); // DELETE
send(204);
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = `http://127.0.0.1:${server.address().port}`;
async function call(method, path, payload) {
const res = await fetch(base + path, {
method,
headers: payload ? { "Content-Type": "application/json" } : {},
body: payload ? JSON.stringify(payload) : undefined,
});
const text = await res.text();
const allow = res.headers.get("allow");
const parts = [method.padEnd(7), path.padEnd(9), res.status];
if (text) parts.push(text);
if (allow) parts.push(`[Allow: ${allow}]`);
console.log(parts.join(" "));
}
await call("POST", "/notes", { title: "Buy milk" });
await call("POST", "/notes", { title: "Buy milk" });
await call("PUT", "/notes/1", { title: "Buy oat milk", done: true });
await call("PUT", "/notes/1", { title: "Buy oat milk", done: true });
await call("PUT", "/notes/1", { title: "Buy oat milk" });
await call("PATCH", "/notes/1", { done: true });
await call("DELETE", "/notes/2");
await call("DELETE", "/notes/2");
await call("DELETE", "/notes");
await call("OPTIONS", "/notes/1");
await call("GET", "/notes");
server.close();Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- 405 with Allow
return send(405, { error: "method_not_allowed" }, { Allow: allow.join(", ") });URL တစ်ခုချင်းစီမှာ ခွင့်ပြုတဲ့ method စာရင်း (allow) ရှိပါတယ်။ ဒီစာရင်းထဲ မပါတဲ့ method ကို 405 နဲ့ ပြန်ပြီး Allow header ထဲ ထောက်ပံ့တဲ့ method တွေကို ပြောပြပါတယ်။ ဒါက 404 နဲ့ မတူပါဘူး: URL မှန်ပေမဲ့ method မမှန်တာပါ။
- POST creates
return send(201, note, { Location: `/notes/${note.id}` });POST က note အသစ်တစ်ခု ဖန်တီးပြီး server က id ကို ရွေးပါတယ်။ Response က 201 Created ဖြစ်ပြီး Location header က အသစ်ရဲ့ URL ကို ပြောပြပါတယ်။ အဲဒါကြောင့် တူညီတဲ့ POST နှစ်ခါ ပို့ရင် note နှစ်ခု ရပါတယ်။
- PUT replaces
notes.set(id, { id, title: body.title, done: body.done ?? false });PUT က note ကို body ထဲက အချက်အလက်နဲ့ အပြည့်အစုံ အစားထိုးပါတယ်။ body မှာ done မပါရင် false အဖြစ် reset ဖြစ်ပါတယ်။ တူညီတဲ့ PUT ကို ထပ်ပို့လည်း နောက်ဆုံး state က အတူတူပဲ ဖြစ်လို့ idempotent ပါ။
- PATCH merges
Object.assign(existing, body, { id });PATCH က ရှိပြီးသား note ထဲကို ပို့လိုက်တဲ့ field တွေကိုပဲ ပေါင်းထည့်ပါတယ်။ { id } ကို နောက်ဆုံးမှာ ထည့်ထားတာက client က id ကို ပြောင်းခိုင်းလည်း မရအောင် ကာကွယ်တာပါ။
- DELETE has no body
send(204);204 No Content ဆိုတာ အောင်မြင်ပြီး body မရှိဘူးလို့ ဆိုလိုပါတယ်။ ဒါကြောင့် send helper က body မရှိရင် Content-Type ကိုလည်း မထည့်ပါဘူး။ ဒုတိယ DELETE က note မရှိတော့လို့ 404 ပြန်ပေမဲ့ server state က မပြောင်းပါဘူး။
- OPTIONS answers Allow
if (req.method === "OPTIONS") return send(204, undefined, { Allow: allow.join(", ") });OPTIONS က ဒီ URL မှာ ဘာ method တွေ ခွင့်ပြုလဲ မေးတာပါ။ Server က body မပါဘဲ Allow header နဲ့ ပြန်ဖြေပါတယ်။ Browser တွေက cross-origin request မတိုင်ခင် OPTIONS ကို အလိုလို ပို့တတ်ပါတယ်။
POST /notes 201 {"id":1,"title":"Buy milk","done":false}
POST /notes 201 {"id":2,"title":"Buy milk","done":false}
PUT /notes/1 200 {"id":1,"title":"Buy oat milk","done":true}
PUT /notes/1 200 {"id":1,"title":"Buy oat milk","done":true}
PUT /notes/1 200 {"id":1,"title":"Buy oat milk","done":false}
PATCH /notes/1 200 {"id":1,"title":"Buy oat milk","done":true}
DELETE /notes/2 204
DELETE /notes/2 404 {"error":"not_found"}
DELETE /notes 405 {"error":"method_not_allowed"} [Allow: GET, POST, OPTIONS]
OPTIONS /notes/1 204 [Allow: GET, PUT, PATCH, DELETE, OPTIONS]
GET /notes 200 [{"id":1,"title":"Buy oat milk","done":true}]Idempotency: retry ကို ဘာကြောင့် ဂရုစိုက်ရလဲ
Network ပျက်လို့ response ပြန်မရောက်တဲ့အခါ client က request ဟာ server ကို ရောက်ခဲ့လား မရောက်ခဲ့လား မသိပါဘူး။ PUT ဒါမှမဟုတ် DELETE ဆိုရင် ပြန်ပို့ရုံပဲ၊ ဘာမှ မပျက်ပါဘူး။ POST ဆိုရင်တော့ ပြန်ပို့တာ order နှစ်ခု၊ ငွေနှစ်ကြိမ် ထုတ်တာ ဖြစ်နိုင်ပါတယ်။ ဖြေရှင်းနည်းက client က request တစ်ခုချင်းစီကို Idempotency-Key ဆိုတဲ့ တစ်မူထူးခြားတဲ့ တန်ဖိုးနဲ့ ပို့ပြီး server က key တူရင် ပထမအဖြေကိုပဲ ပြန်ပေးတာပါ။
import http from "node:http";
const orders = [];
const saved = new Map(); // Idempotency-Key -> the answer already given
const server = http.createServer((req, res) => {
const key = req.headers["idempotency-key"];
res.writeHead(201, { "Content-Type": "application/json" });
if (key && saved.has(key)) return res.end(saved.get(key)); // replay, create nothing
const order = { id: orders.length + 1 };
orders.push(order);
const answer = JSON.stringify(order);
if (key) saved.set(key, answer);
res.end(answer);
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = `http://127.0.0.1:${server.address().port}`;
// A client that retries a POST three times, as if the first replies were lost.
async function checkout(headers) {
const res = await fetch(`${base}/orders`, { method: "POST", headers });
return res.text();
}
for (let attempt = 0; attempt < 3; attempt++) await checkout({});
console.log("without a key:", orders.length, "orders created");
orders.length = 0;
for (let attempt = 0; attempt < 3; attempt++) {
await checkout({ "Idempotency-Key": "checkout-42" });
}
console.log("with a key :", orders.length, "order created");
server.close();Key မပါရင် ပြန်ပို့တာ သုံးခါစလုံး order အသစ် ဖန်တီးလို့ သုံးခု ရပါတယ်။ Key ပါရင် ပထမတစ်ခါပဲ order ဖန်တီးပြီး ကျန်နှစ်ခါက သိမ်းထားတဲ့ အဖြေကို ပြန်ရလို့ တစ်ခုပဲ ရှိပါတယ်။ Idempotency-Key က payment API တွေမှာ အသုံးများပါတယ်၊ ဒါပေမဲ့ API တစ်ခုချင်းစီက ထောက်ပံ့မထောက်ပံ့ သူ့ documentation မှာ စစ်ပါ။
PUT လား PATCH လား
| ကွာခြားချက် | PUT | PATCH |
|---|---|---|
| ဘာပို့လဲ | Resource ရဲ့ ပုံစံ အပြည့်အစုံ | ပြောင်းချင်တဲ့ field တွေပဲ |
| မပါတဲ့ field | ဖယ်ထုတ် ဒါမှမဟုတ် reset ဖြစ် | မထိဘဲ ကျန် |
| ထပ်ပို့ရင် | Idempotent | လုပ်ဆောင်ချက်ပေါ် မူတည် (set ဆို ရ၊ increment ဆို မရ) |
Profile တစ်ခုမှာ နာမည်တစ်ခုတည်း ပြောင်းချင်ရင် PATCH က ပိုသင့်တော်ပါတယ်။ PUT ကို နာမည်တစ်ခုတည်း ပို့မိရင် email နဲ့ phone ကဲ့သို့ ကျန်တဲ့ field တွေ ပျောက်သွားနိုင်ပါတယ်။ PUT သုံးမယ်ဆိုရင် အရင် GET လုပ်၊ field ပြင်၊ ပြီးမှ ပုံစံအပြည့်အစုံကို ပြန်ပို့ပါ။
405, OPTIONS နဲ့ browser
405 Method Not Allowed ကို Allow header နဲ့ အတူ ပြန်ရမယ်လို့ HTTP က ပြောထားပါတယ်၊ ဒါမှ client က ဘာတွေ သုံးလို့ရလဲ သိမှာပါ။ 404 ကတော့ URL မရှိတာ၊ 405 ကတော့ URL ရှိပေမဲ့ ဒီ method မရတာ ဖြစ်ပါတယ်။ OPTIONS ကို curl မှာ ကိုယ်တိုင် ပို့ကြည့်လို့ရပေမဲ့ browser တွေက cross-origin request တချို့မတိုင်ခင် preflight အနေနဲ့ အလိုလို ပို့ပါတယ်။
ဒါကြောင့် API ကို curl မှာ အလုပ်လုပ်ပေမဲ့ browser မှာ မအလုပ်လုပ်တဲ့ ပြဿနာတွေရဲ့ အကြောင်းရင်းက server က OPTIONS ကို မဖြေတာ ဖြစ်တတ်ပါတယ်။ အသေးစိတ်ကို နောက်ပိုင်း CORS lesson မှာ လေ့လာပါမယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ HTTP method
REST API အများစုက CRUD ကို method တွေနဲ့ ချိတ်ပါတယ်: POST က Create၊ GET က Read၊ PUT/PATCH က Update၊ DELETE က Delete။ OpenAPI documentation မှာလည်း path တစ်ခုချင်းစီအောက်မှာ method တွေ ခွဲပြထားပါတယ်။ ဒါကြောင့် method ကို မှန်မှန်ကန်ကန် ရွေးထားရင် documentation ကိုမဖတ်ဘဲတောင် API ကို ခန့်မှန်းလို့ ရပါတယ်။
- Payment နဲ့ order API တွေက POST ကို Idempotency-Key နဲ့ တွဲပါတယ်၊ ဒါမှ network retry ကြောင့် ငွေနှစ်ကြိမ် မထွက်ပါဘူး။
- HTTP client နဲ့ proxy တချို့က idempotent လို့ သတ်မှတ်ထားတဲ့ request တွေကို ချိတ်ဆက်မှု ပြတ်ရင် အလိုလို ပြန်ကြိုးစားပေမဲ့ POST ကိုတော့ မလုပ်ပါဘူး၊ ဒီအပြုအမူကို မှီခိုထားတာကြောင့် method မှန်ဖို့ အရေးကြီးပါတယ်။
- Browser မှာ form တစ်ခုကို POST နဲ့ submit ပြီးနောက် refresh လုပ်ရင် 'ပြန်ပို့မလား' လို့ မေးတာ POST က ထပ်လုပ်ရင် အန္တရာယ်ရှိနိုင်လို့ပါ၊ GET ကတော့ မမေးပါဘူး။
- Crawler, CDN နဲ့ cache တွေက GET ကိုပဲ cache လုပ်ပါတယ်၊ ဒါကြောင့် ဖတ်တာကို GET နဲ့ရေးမှ ပိုမြန်ပြီး ကုန်ကျစရိတ်လည်း သက်သာပါတယ်။
အမှားများ
HTTP method နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ
GET ကို state ပြောင်းဖို့ သုံးခြင်း
/orders/7/cancel လို GET link တစ်ခုကို crawler, preview bot ဒါမှမဟုတ် prefetch က ဖွင့်ရုံနဲ့ ဖျက်ပစ်ပါတယ်။ ပြောင်းလဲမှုတိုင်းက POST, PUT, PATCH ဒါမှမဟုတ် DELETE ထဲမှာ ရှိရပါမယ်။
PUT ကို တစ်စိတ်တစ်ပိုင်း ပြင်ဖို့ သုံးခြင်း
Body မှာ ပြင်ချင်တဲ့ field တစ်ခုပဲ ထည့်ပြီး PUT နဲ့ ပို့ရင် ကျန်တဲ့ field တွေ ပျောက်ဒါမှမဟုတ် default ပြန်ဖြစ်သွားနိုင်ပါတယ်။ တစ်စိတ်တစ်ပိုင်းဆိုရင် PATCH၊ အစားထိုးဆိုရင် PUT ကို ရွေးပါ။
POST ကို မစဉ်းစားဘဲ ပြန်ပို့ခြင်း (retry)
Timeout ဖြစ်ရင် client library က POST ကို အလိုလို ပြန်ပို့တာ ဒါမှမဟုတ် 'ကြိုးစားမယ်' button ကို နှိပ်တာက ထပ်တူ order နှစ်ခု ဖန်တီးနိုင်ပါတယ်။ Idempotency-Key သုံးပါ ဒါမှမဟုတ် server ဘက်မှာ duplicate ကို ဖမ်းပါ။
GET ကို body နဲ့ ပို့ခြင်း
GET request ရဲ့ body ကို HTTP က အဓိပ္ပာယ် သတ်မှတ်မထားပါဘူး၊ proxy, cache နဲ့ server တချို့က လွှင့်ပစ်ဒါမှမဟုတ် ငြင်းပယ်ပါတယ်။ ရှာဖွေတာ စစ်ထုတ်တာတွေကို query string နဲ့ ပို့ပါ။
Best Practices
Best practices
- URL ကို နာမ်စား (resource) အဖြစ်ထားပြီး လုပ်ဆောင်ချက်ကို method နဲ့ ပြောပါ: DELETE /notes/2 ကောင်းပြီး GET /deleteNote?id=2 မကောင်းပါဘူး။
- POST က 201 နဲ့ Location ကို ပြန်ပါ၊ DELETE က 204 ပြန်ပါ၊ မထောက်ပံ့တာကို 405 နဲ့ Allow ပြန်ပါ။
- Retry လုပ်ရမယ့် POST တွေအတွက် Idempotency-Key ကို ထောက်ပံ့ပါ။
- PATCH ရဲ့ ရလဒ်ကို idempotent ဖြစ်အောင် ဒီဇိုင်းလုပ်ပါ (တိုး ထက် set လုပ်တာ ပိုကောင်း)။
Mini challenge
Mini challenge: counter API နဲ့ idempotency ကို မျက်မြင်ကြည့်ခြင်း
/counter ဆိုတဲ့ resource တစ်ခုပါတဲ့ server ရေးပါ။ POST /counter/increment က 1 တိုးပါတယ်၊ PUT /counter က { value } ကို set လုပ်ပါတယ်၊ DELETE /counter က 0 ကို reset လုပ်ပြီး 204 ပြန်ပါတယ်၊ GET /counter က ဖတ်ပါတယ်။ Request တွေကို နှစ်ခါစီ ပို့ပြီး ဘယ်ဟာတွေက ရလဒ်ပြောင်းလဲလဲ ကြည့်ပါ။
- POST /counter/increment ကို POST မဟုတ်ရင် 405 နဲ့ Allow: POST ပြန်ရမယ်
- /counter ရဲ့ GET, PUT, DELETE မှလွဲပြီး method တွေက 405 နဲ့ Allow: GET, PUT, DELETE ပြန်ရမယ်
- DELETE ကို body မပါဘဲ 204 ပြန်ရမယ်
- POST နှစ်ခါ၊ PUT နှစ်ခါ၊ DELETE နှစ်ခါ၊ GET နဲ့ PATCH တစ်ခါ ပို့ပြီး တစ်ကြောင်းစီ print ထုတ်ပါ
import http from "node:http";
// TODO: a counter variable and a server on port 0
// TODO: POST /counter/increment adds 1 and answers { value }
// any other method on that URL -> 405 with an Allow header
// TODO: /counter supports GET (read), PUT (set { value }) and DELETE (reset, 204)
// every other method -> 405 with Allow: GET, PUT, DELETE
// TODO: a call(method, path, payload) helper that prints method, path, status, body
// TODO: run: POST twice, PUT {value: 10} twice, DELETE twice, GET, then PATCH
// and notice which repeated requests changed the outcomeရလဒ်: POST နှစ်ခါက {"value":1} နဲ့ {"value":2}၊ PUT နှစ်ခါက {"value":10} နှစ်ခါစလုံး၊ DELETE နှစ်ခါက 204 နှစ်ခါ၊ GET က {"value":0}၊ PATCH က 405 နဲ့ Allow: GET, PUT, DELETE ဖြစ်ရမယ်။
အရိပ်အမြွက် ကြည့်ရန်
switch (req.method) နဲ့ /counter ကို ခွဲပါ၊ default case က 405 ဖြစ်ပါမယ်။ PUT body ကို ဖတ်ဖို့ request ကို for await ပတ်ပြီး စုထားပါ။ 204 မှာ body မရေးပါနဲ့။
အဖြေ ကြည့်ရန်
import http from "node:http";
let counter = 0;
const server = http.createServer(async (req, res) => {
let raw = "";
for await (const chunk of req) raw += chunk;
const send = (status, body, headers = {}) => {
const type = body === undefined ? {} : { "Content-Type": "application/json" };
res.writeHead(status, { ...type, ...headers });
res.end(body === undefined ? undefined : JSON.stringify(body));
};
if (req.url === "/counter/increment") {
if (req.method !== "POST") return send(405, { error: "use POST" }, { Allow: "POST" });
counter += 1;
return send(200, { value: counter });
}
if (req.url !== "/counter") return send(404, { error: "not_found" });
switch (req.method) {
case "GET":
return send(200, { value: counter });
case "PUT":
counter = JSON.parse(raw).value;
return send(200, { value: counter });
case "DELETE":
counter = 0;
return send(204);
default:
return send(405, { error: "method_not_allowed" }, { Allow: "GET, PUT, DELETE" });
}
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = `http://127.0.0.1:${server.address().port}`;
async function call(method, path, payload) {
const res = await fetch(base + path, {
method,
headers: payload ? { "Content-Type": "application/json" } : {},
body: payload ? JSON.stringify(payload) : undefined,
});
const allow = res.headers.get("allow");
const text = await res.text();
const parts = [`${method} ${path}`.padEnd(24), res.status];
if (text) parts.push(text);
if (allow) parts.push(`Allow: ${allow}`);
console.log(parts.join(" "));
}
await call("POST", "/counter/increment");
await call("POST", "/counter/increment");
await call("PUT", "/counter", { value: 10 });
await call("PUT", "/counter", { value: 10 });
await call("DELETE", "/counter");
await call("DELETE", "/counter");
await call("GET", "/counter");
await call("PATCH", "/counter", { value: 1 });
server.close();Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Method က URL ပေါ်မှာ ဘာလုပ်မလဲ ပြောပါတယ်: GET ဖတ်၊ POST ဖန်တီး၊ PUT အစားထိုး၊ PATCH တစ်စိတ်တစ်ပိုင်းပြင်၊ DELETE ဖယ်။
- Safe method (GET, HEAD, OPTIONS) က state မပြောင်းပါဘူး၊ idempotent method (PUT, DELETE နဲ့ safe တွေ) ကို ထပ်ပို့လို့ ရပါတယ်၊ POST ကိုတော့ မရပါဘူး။
- Idempotent ဆိုတာ နောက်ဆုံး state တူတာ၊ response တူရမယ်လို့ မဟုတ်ပါဘူး (ဒုတိယ DELETE က 404 ဖြစ်နိုင်)။
- မထောက်ပံ့တဲ့ method ကို 405 နဲ့ Allow header ပြန်ပါ၊ POST retry အတွက် Idempotency-Key သုံးပါ။
RFC 9110 — HTTP Semantics — IETF