Thuta Learning
API Tutorial
BasicWeb Developmentအလယ်အလတ်

Endpoint ပုံစံ (URL/Params/Headers/Body)

API Tutorialသင်ခန်းစာ 530 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • URL ကို scheme, host, port, path, query string, fragment ခွဲပြီး ဘယ်အပိုင်းတွေ server ဆီ ရောက်လဲ (fragment မရောက်ဘူး) ရှင်းပြနိုင်ဖို့
  • URLSearchParams နဲ့ encodeURIComponent ကို သုံးပြီး query string ကို ဘေးကင်းစွာ တည်ဆောက်နိုင်ဖို့
  • fetch request မှာ Accept, Content-Type နဲ့ Authorization header တွေ သတ်မှတ်နိုင်ဖို့
  • JSON body ကို မှန်ကန်တဲ့ Content-Type နဲ့ ပို့နိုင်ပြီး မပါရင် ဘာဖြစ်လဲ ရှင်းပြနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Endpoint လိပ်စာဆိုတာ စာတိုက်လိပ်စာနဲ့ ညွှန်ကြားချက်တွေ ပေါင်းထားတာလိုပါပဲ။ URL က ဘယ်အဆောက်အအုံ၊ ဘယ်အခန်းလဲ (host နဲ့ path) ကို ပြောပါတယ်။ Query string က "စာမျက်နှာ ၂၊ ပစ္စည်း နှစ်ဆယ်" လို အသေးစိတ်ရေးထားတဲ့ sticky note ပါ။ Header တွေက ကတ်ထူအိတ်ပေါ်က စာတွေပါ — ဘယ်သူပို့လဲ၊ ဘယ်ဘာသာစကား ဖတ်နိုင်လဲ၊ ကိုယ့်ကိုယ်ကိုယ် ဘယ်လို သက်သေပြလဲ။ Body ကတော့ အိတ်ထဲက စာပါ။ ဒီလေးခုထဲက တစ်ခုခု မှားနေ ဒါမှမဟုတ် ပျောက်နေရင် စာတိုက်က ပြန်ပို့တာ ဒါမှမဟုတ် အခန်းမှားပို့တာ ဖြစ်ပါလိမ့်မယ်။

Problem → Why → Solution

ပြဿနာ: string ဆက်ပြီး ဆောက်တဲ့ URL က တိတ်တိတ်ပုန်း ပျက်တယ်

Search box က user က fish & chips လို့ ရိုက်မချင်း အလုပ်လုပ်ပါတယ်။ Code က URL ကို string တွေ ဆက်ပြီး ဆောက်ထားပါတယ် — base + "/search?q=" + term ပါ။ & က "fish " ပြီးတဲ့နောက် q parameter ကို အဆုံးသတ်ပြီး chips ဆိုတဲ့ parameter အလွတ်တစ်ခု စတင်ပါတယ်။ Server က "fish " ကို ရှာပေးလို့ user က မှားတဲ့ ရလဒ်ကို ရပြီး error ဆိုတာ လုံးဝ မပေါ်ပါဘူး။ Space၊ #၊ + နဲ့ ASCII မဟုတ်တဲ့ စာလုံးတွေမှာလည်း ဒီ bug မျိုးစုံ ပေါ်ပါတယ်။

ဒုတိယ ဆိုးဆိုးရွားရွား ထုံးစံတစ်ခုက fetch နဲ့ JSON ပို့ပြီး Content-Type header ကို မေ့တာပါ။ fetch က string body ကို text/plain လို့ တံဆိပ်တပ်ပြီး JSON API က ငြင်းတာ ဒါမှမဟုတ် လျစ်လျူရှုတာကြောင့် body မှန်နေတဲ့ ကြားက response က 415 ဒါမှမဟုတ် 400 ပြန်ပါတယ်။

joined-url.mjs
const term = "fish & chips";
const url = "https://api.example.com/v1/search?q=" + term + "&page=2";
console.log(url);
// https://api.example.com/v1/search?q=fish & chips&page=2
// The server reads q = "fish " plus an extra parameter named " chips".

ဘာကြောင့် ဒီလို ပျက်တာလဲ

URL ဆိုတာ အထူးအက္ခရာတွေ ပါတဲ့ string ပါ — ? က query စတယ်၊ & က parameter တွေကို ခွဲတယ်၊ = က နာမည်နဲ့ တန်ဖိုးကို ခွဲတယ်၊ # က fragment စတယ်၊ / က path အပိုင်းတွေကို ခွဲတယ်။ ကိုယ့် data ထဲမှာ ဒီအက္ခရာတစ်ခု ပါနေရင် server က data နဲ့ ဖွဲ့စည်းပုံကို ခွဲလို့ရအောင် percent-encode လုပ်ရပါမယ်။ Header တန်ဖိုးနဲ့ body မှာလည်း ကိုယ်ပိုင် စည်းမျဉ်း ရှိပါတယ် — body ဆိုတာ byte တွေပဲဖြစ်ပြီး Content-Type header တစ်ခုတည်းက server ကို ဘယ်လို ဖတ်ရမလဲ ပြောပါတယ်။

Core concept: URL၊ header နဲ့ body ရဲ့ တာဝန်တွေ

URL မှာ အပိုင်း ခြောက်ပိုင်းအထိ ရှိပါတယ်။ https://api.example.com:8443/v1/users/42?include=posts&page=2#profile ထဲမှာ https က scheme (protocol)၊ api.example.com က host၊ 8443 က port (scheme ရဲ့ default ဖြစ်ရင် မရေးပါဘူး — https က 443၊ http က 80)၊ /v1/users/42 က resource ကို ဖော်ထုတ်တဲ့ path၊ include=posts&page=2 က query string၊ profile က fragment ပါ။ Fragment က ထူးခြားပါတယ် — browser တွေက ကိုယ်တိုင်ပဲ သိမ်းထားပြီး server ဆီ ဘယ်တော့မှ မပို့ပါဘူး၊ ဒါကြောင့် API က fragment ကို ဘယ်တော့မှ ဖတ်လို့ မရပါဘူး။

Identity ကို path မှာ၊ option တွေကို query မှာ ထည့်ပါ။ /users/42 က ဘယ် user လဲ ပြောပါတယ်။ ?include=posts&page=2 က အဖြေရဲ့ ပုံစံကို ချိန်ညှိပါတယ် — filter၊ sort၊ paging၊ field ရွေးချယ်ခြင်း။ Query string ဆိုတာ & နဲ့ ဆက်ထားတဲ့ name=value စုံတွေရဲ့ စာရင်းပါ၊ နာမည်တစ်ခု ထပ်နိုင်ပါတယ် (?tag=a&tag=b)။ Path parameter တွေက resource ကို ဖော်ထုတ်ဖို့ မဖြစ်မနေလိုပြီး query parameter တွေက များသောအားဖြင့် default ပါတဲ့ optional တွေပါ။

URL ထဲက data ထဲမှာ ဖွဲ့စည်းပုံအဓိပ္ပာယ်ရှိတဲ့ အက္ခရာတွေ ပါလာရင် percent-encode လုပ်ရပါမယ် — space က %20 (ဒါမှမဟုတ် form ပုံစံ query string မှာ +)၊ & က %26၊ ASCII မဟုတ်တဲ့ စာလုံးက UTF-8 byte တွေကို %XX အဖြစ်ပါ။ JavaScript မှာ တန်ဖိုးတစ်ခုချင်းအတွက် encodeURIComponent(value) ကို သုံးပါ၊ ဒါထက် ပိုကောင်းတာက URLSearchParams ကို query string တစ်ခုလုံး တည်ဆောက်ခိုင်းတာပါ၊ ဒါမှ မေ့မှာ မဟုတ်ပါဘူး။ URL တစ်ခုလုံးကို encodeURIComponent မလုပ်ပါနဲ့ (slash နဲ့ colon တွေကိုပါ encode လုပ်သွားပါလိမ့်မယ်)၊ တန်ဖိုးတစ်ခုကို နှစ်ခါ မ encode ပါနဲ့ — % က %25 ဖြစ်ပြီး server က အဓိပ္ပာယ်မရှိတာ မြင်ရပါလိမ့်မယ်။

Header တွေက request အကြောင်း metadata ကို name: value လိုင်းတွေအဖြစ် သယ်ပါတယ်။ API call အားလုံးလိုလိုမှာ သုံးခု အရေးကြီးပါတယ်။ Accept က ကိုယ်လက်ခံမယ့် format တွေကို ပြောပါတယ် (ဥပမာ application/json)။ Content-Type က ပို့မယ့် body ရဲ့ format ကို ပြောပါတယ်။ Authorization က credential ကို သယ်ပါတယ် (ဥပမာ Bearer နောက်မှ token)။ Header နာမည်တွေက စာလုံးအကြီးအသေး မခွဲပေမယ့် token လို တန်ဖိုးတွေက ခွဲပါတယ်။ Secret တွေကို URL ထဲ မထည့်ပါနဲ့ — URL တွေက browser history၊ server log နဲ့ referrer header ထဲ ရောက်လေ့ရှိပေမယ့် Authorization header ကတော့ ပုံမှန်အားဖြင့် မရောက်ပါဘူး။

Body ကတော့ data ပို့တဲ့ method တွေ (အများအားဖြင့် POST, PUT, PATCH) အတွက် payload ပါ။ GET request မှာ အဓိပ္ပာယ်ရှိတဲ့ body မရှိပါဘူး။ JSON API တွေမှာ body က JSON စာသားဖြစ်ပြီး Content-Type က application/json လို့ ပြောရပါမယ်၊ မဟုတ်ရင် server က 415 Unsupported Media Type နဲ့ ငြင်းတာ ဒါမှမဟုတ် တိတ်တိတ်ပဲ လျစ်လျူရှုတာ ဖြစ်နိုင်ပါတယ်။ fetch မှာ string body ကို ကိုယ်တိုင် header မသတ်မှတ်ရင် text/plain;charset=UTF-8 လို့ တံဆိပ်တပ်ပါတယ်၊ ဒါကြောင့် ဒီ header က "ငါ့ body အလွတ်ရောက်တယ်" bug တွေရဲ့ အကြောင်းရင်း နံပါတ်တစ်ပါ။

မှတ်ထားရမယ့် ပုံစံ: တံဆိပ်လေးခုပါတဲ့ ပါဆယ်
  1. URL: ပါဆယ် ဘယ်သွားမလဲ
  2. Query: ကိုင်တွယ်ရမယ့် ညွှန်ကြားချက်
  3. Header: အပြင်ဘက်က တံဆိပ်
  4. Body: သေတ္တာထဲက အရာ

URL, query နဲ့ header တွေက server ကို ကြိုမြင်ရပြီး body ကိုတော့ Content-Type က ဖတ်နည်း ပြောပါတယ်။

အထွေထွေ ပုံစံ: URL နဲ့ request ရဲ့ ပုံသဏ္ဌာန်

text
scheme://host[:port]/path/to/resource?name=value&name2=value2#fragment

Request line:  METHOD /path?query HTTP/1.1
Headers:       Name: value        (one per line)
               (a blank line ends the headers)
Body:          optional payload

The fragment (#...) stays in the client and is never sent.

Server ဆီ ရောက်တာက request line (method, path, query)၊ header တွေ နဲ့ body ပါ။ Host က header အဖြစ် ရောက်ပါတယ်။ Fragment ကတော့ ပထမ line မှာတောင် မပါပါဘူး။ Blank line တစ်ကြောင်းက header တွေနဲ့ body ကို ခွဲပါတယ်။

Example: URL ကို ခွဲပြီး server ဘာမြင်လဲ ကြည့်မယ်

ဒီ script က အပိုင်းသုံးပိုင်း ရှိပါတယ်။ ပထမ၊ URL object နဲ့ URL တစ်ခုကို အပိုင်းလိုက် ခွဲပါတယ် (api.example.com က ဥပမာ host ပါ)။ ဒုတိယ၊ server ဆီ တကယ် ရောက်တာတွေကို ပြန်ပြတဲ့ mock server ကို စတင်ပါတယ်။ တတိယ၊ URL နဲ့ URLSearchParams သုံးပြီး ဘေးကင်းစွာ request တည်ဆောက်ပါတယ် — query ထဲမှာ fish & chips ပါပြီး fragment လည်း ထည့်ထားပါတယ်။ Output ရဲ့ "raw URL" လိုင်းမှာ fragment မပါတာကို သတိထားကြည့်ပါ။

endpoint-parts.mjs
import http from "node:http";

// 1. Take a URL apart (api.example.com is a placeholder host).
const u = new URL(
  "https://api.example.com:8443/v1/users/42?include=posts&page=2#profile"
);
console.log("scheme  :", u.protocol);
console.log("host    :", u.hostname);
console.log("port    :", u.port);
console.log("path    :", u.pathname);
console.log("query   :", u.search);
console.log("fragment:", u.hash);

// 2. A mock server that reports which parts of the request actually reached it.
const server = http.createServer((req, res) => {
  const url = new URL(req.url, "http://localhost");
  let raw = "";
  req.on("data", (chunk) => (raw += chunk));
  req.on("end", () => {
    res.writeHead(200, { "Content-Type": "application/json" });
    res.end(
      JSON.stringify({
        rawUrl: req.url,
        q: url.searchParams.get("q"),
        page: url.searchParams.get("page"),
        accept: req.headers.accept,
        contentType: req.headers["content-type"],
        body: raw,
      })
    );
  });
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = `http://127.0.0.1:${server.address().port}`;

// 3. Build the request safely: URL for the address, URLSearchParams for the query.
const target = new URL("/v1/search", base);
target.searchParams.set("q", "fish & chips");
target.searchParams.set("page", "2");
target.hash = "results"; // the fragment stays on the client
const res = await fetch(target, {
  method: "POST",
  headers: { Accept: "application/json", "Content-Type": "application/json" },
  body: JSON.stringify({ sort: "price" }),
});
const seen = await res.json();
console.log("raw URL  :", seen.rawUrl);
console.log("q decoded:", seen.q);
console.log("page     :", seen.page);
console.log("accept   :", seen.accept);
console.log("type     :", seen.contentType);
console.log("body     :", seen.body);

server.close();

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Parsing a URL"https://api.example.com:8443/v1/users/42?include=posts&page=2#profile"

    URL class က string ကို အပိုင်းလိုက် ခွဲပေးပါတယ်၊ ကိုယ်တိုင် regular expression ရေးစရာ မလိုပါဘူး။ protocol မှာ https: လို့ colon ပါပြီး search မှာ ? ပါပြီး hash မှာ # ပါတာကို သတိထားပါ။

  2. Server-side parsingconst url = new URL(req.url, "http://localhost");

    Server မှာ req.url က path နဲ့ query ကိုပဲ ပေးတာမို့ (host မပါ) URL class နဲ့ ခွဲဖို့ base တစ်ခု လိုပါတယ်။ "http://localhost" က အဲဒီအတွက်ပဲ ဖြစ်ပြီး ရလဒ်ကို မထိခိုက်ပါဘူး။

  3. Decoded valueq: url.searchParams.get("q"),

    searchParams.get က encode လုပ်ထားတာတွေကို ပြန်ဖြေပေးပါတယ်၊ ဒါကြောင့် fish+%26+chips က fish & chips အဖြစ် ပြန်ရပါတယ်။ + ကို space လို့ဖတ်တာ URLSearchParams ရဲ့ စံပါ။

  4. Safe query buildingtarget.searchParams.set("q", "fish & chips");

    တန်ဖိုးကို ကိုယ်တိုင် encode မလုပ်ပါဘူး၊ searchParams က & ကို %26 နဲ့ space ကို + နဲ့ အလိုအလျောက် ပြောင်းပေးပါတယ်။ Problem section က string ဆက်နည်းနဲ့ ဒီလိုင်းကို နှိုင်းယှဉ်ကြည့်ပါ။

  5. Fragment stays hometarget.hash = "results"; // the fragment stays on the client

    Fragment ကို URL object မှာ သတ်မှတ်လို့ရပေမယ့် fetch က ပို့တဲ့အခါ ဖယ်ထားပါတယ်။ ဒါကြောင့် output ရဲ့ raw URL မှာ #results မပါပါဘူး။

  6. Headersheaders: { Accept: "application/json", "Content-Type": "application/json" },

    Accept က JSON ကို လက်ခံကြောင်း၊ Content-Type က body က JSON ဖြစ်ကြောင်း ပြောပါတယ်။ Server ဘက်က accept နဲ့ contentType ကို ပြန်ဖတ်ပြီး ပြန်ပြပါတယ်။

  7. The bodybody: JSON.stringify({ sort: "price" }),

    Body က JSON စာသားပါ၊ ဒါကြောင့် server မှာ body: {"sort":"price"} လို့ ပေါ်ပါတယ်။ Query ထဲက page နဲ့ body ထဲက sort ကို မရောပါနဲ့ — နေရာတစ်ခုစီမှာ ကိုယ်ပိုင် တာဝန်ရှိပါတယ်။

You should see
scheme  : https:
host    : api.example.com
port    : 8443
path    : /v1/users/42
query   : ?include=posts&page=2
fragment: #profile
raw URL  : /v1/search?q=fish+%26+chips&page=2
q decoded: fish & chips
page     : 2
accept   : application/json
type     : application/json
body     : {"sort":"price"}

encodeURIComponent နဲ့ URLSearchParams ကွာခြားချက်

ကိရိယာတစ်ခုစီက အနည်းငယ် ကွာတဲ့ ရလဒ်ကို ပေးပါတယ်။ ဒီ script ကို run ပြီး တစ်ခုချင်းကို ယှဉ်ကြည့်ပါ။ Output ထဲမှာ encodeURIComponent က space ကို %20 နဲ့ ပြောင်းပြီး URLSearchParams ကတော့ + နဲ့ ပြောင်းတာကို တွေ့ရပါမယ်။ နှစ်မျိုးလုံး server ဘက်မှာ မှန်မှန်ကန်ကန် ပြန်ဖတ်လို့ရပါတယ်။

encoding.mjs
const term = "fish & chips/é";
console.log(encodeURIComponent(term));
console.log(new URLSearchParams({ q: term }).toString());
console.log(encodeURI("https://api.example.com/a b?x=1&y=é"));
console.log(decodeURIComponent("fish%20%26%20chips"));
console.log(encodeURIComponent(encodeURIComponent("a b")));
ကိရိယာဘာအတွက် သုံးလဲSpace က
encodeURIComponentတန်ဖိုးတစ်ခု ဒါမှမဟုတ် path အပိုင်းတစ်ခု%20
URLSearchParamsQuery string တစ်ခုလုံး+
encodeURIURL အပြည့်အစုံ (? နဲ့ & ကို မထိ)%20

နောက်ဆုံး line က အမှားတစ်ခုကို ပြပါတယ် — a b ကို နှစ်ခါ encode လုပ်လိုက်တော့ a%2520b ရတယ်၊ ပထမ encode ရဲ့ % ကို ဒုတိယက %25 လို့ ပြောင်းလိုက်တာပါ။ Server က decode တစ်ခါပဲ လုပ်လို့ a%20b လို အဓိပ္ပာယ်မရှိတာ မြင်ရပါလိမ့်မယ်။

Content-Type မပါရင် ဘာဖြစ်လဲ

အောက်က mock server က JSON-only API တစ်ခုလို ဆောင်ရွက်ပါတယ် — Content-Type က application/json မဟုတ်ရင် 415 ပြန်ပါတယ်။ Client က အတူတူ body ကို header မပါဘဲ တစ်ကြိမ်၊ header နဲ့ တစ်ကြိမ် ပို့ပါတယ်။ ပထမ request မှာ server မြင်တဲ့ type ကို error message ထဲ ပြန်ပြထားတာကို ကြည့်ပါ။

content-type.mjs
import http from "node:http";

const server = http.createServer((req, res) => {
  let raw = "";
  req.on("data", (chunk) => (raw += chunk));
  req.on("end", () => {
    const type = req.headers["content-type"] ?? "";
    if (!type.startsWith("application/json")) {
      res.writeHead(415, { "Content-Type": "application/json" });
      res.end(JSON.stringify({ error: "expected application/json, got " + type }));
      return;
    }
    res.writeHead(201, { "Content-Type": "application/json" });
    res.end(JSON.stringify({ created: JSON.parse(raw) }));
  });
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const url = `http://127.0.0.1:${server.address().port}/users`;
const payload = JSON.stringify({ name: "Alice" });

const bad = await fetch(url, { method: "POST", body: payload });
console.log(bad.status, await bad.text());

const good = await fetch(url, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: payload,
});
console.log(good.status, await good.text());

server.close();

Output ရဲ့ ပထမလိုင်းက 415 နဲ့ "expected application/json, got text/plain;charset=UTF-8" ဖြစ်ပြီး ဒုတိယလိုင်းက 201 နဲ့ {"created":{"name":"Alice"}} ဖြစ်ပါတယ်။ တခြား server တွေမှာ ငြင်းမယ့်အစား body ကို တိတ်တိတ်ပဲ လျစ်လျူရှုပြီး 400 ဒါမှမဟုတ် အလွတ် object ကို ပေးတာလည်း ရှိပါတယ်။ ဘယ်လိုဖြစ်ဖြစ် header ကို အမြဲ ထည့်ပါ။

အသုံးများတဲ့ header တွေ

Headerဘယ်ဘက်ကရည်ရွယ်ချက်
AcceptRequestကိုယ်ဖတ်နိုင်တဲ့ format (ဥပမာ application/json)
Content-Typeနှစ်ဖက်လုံးပါလာတဲ့ body ရဲ့ format
AuthorizationRequestCredential (ဥပမာ Bearer <token>)
LocationResponseResource အသစ်ရဲ့ URL (201 နောက်မှာ)
Retry-AfterResponseပြန်ကြိုးစားမလုပ်ခင် စောင့်ရမယ့် အချိန်

လက်တွေ့ project တွေမှာ

Real project တွေမှာ endpoint အပိုင်းတွေ

API documentation တိုင်းက endpoint တစ်ခုကို ဒီအပိုင်းတွေအတိုင်း ဖော်ပြပါတယ် — method နဲ့ URL template၊ query parameter table၊ လိုအပ်တဲ့ header၊ body ရဲ့ schema နဲ့ ဥပမာ response ပါ။ Docs ဖတ်တာဆိုတာ ကိုယ့်ရေးမယ့် request ကို အဲဒီအပိုင်းတွေဆီ map လုပ်တာပါပဲ။ Version နဲ့ resource ကို path မှာ (/v1/users) ထားပြီး filter၊ sort၊ page တွေကို query မှာ ထားတဲ့ ပုံစံက အလွန် အသုံးများပါတယ်။

Production မှာ URL တွေက တစ်နေရာထဲမှာ မကပါဘူး၊ load balancer log၊ CDN log၊ browser history၊ monitoring tool တွေထဲ ရောက်ပါတယ်။ ဒါကြောင့် token နဲ့ ကိုယ်ရေးကိုယ်တာ data (အီးမေးလ်၊ ဖုန်းနံပါတ်) ကို URL ထဲ မထည့်ဘဲ header ဒါမှမဟုတ် body ထဲ ထည့်ပါတယ်။ API gateway တွေက host နဲ့ path အရ request တွေကို service အမျိုးမျိုးဆီ လမ်းကြောင်းခွဲပါတယ်။ Browser DevTools ရဲ့ Network tab မှာ request တစ်ခုကို နှိပ်ရင် ဒီအပိုင်းတွေကို သီးခြားစီ တွေ့ရလို့ debug လုပ်ရတာ လွယ်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. String ဆက်ပြီး URL ဆောက်တာ

"?note=" + text မှာ text ထဲက # က URL ကို ဖြတ်လိုက်ပါတယ်၊ ဥပမာ item #3 ဆိုရင် #3 က fragment ဖြစ်ပြီး server ဆီ ဘယ်တော့မှ မရောက်ပါဘူး။ & က parameter အသစ် စတယ်၊ space နဲ့ non-ASCII စာလုံးတွေလည်း ပြဿနာ ဖြစ်ပါတယ်။ URL နဲ့ URLSearchParams ကို အမြဲသုံးပါ။

2. Content-Type ကို မေ့တာ

fetch နဲ့ JSON.stringify(...) ကို body အဖြစ် ပို့ပြီး header မထည့်ရင် server က text/plain လို့ မြင်ပါတယ်။ ရလဒ်က 415၊ 400 ဒါမှမဟုတ် Express လို framework မှာ body အလွတ် (req.body က {}) ဖြစ်ပါတယ်။ JSON ပို့တိုင်း Content-Type: application/json ကို ထည့်ပါ။

3. Secret ကို query string ထဲ ထည့်တာ

?api_key=abc123 လို့ ထည့်ရင် key က log၊ browser history နဲ့ referrer တွေထဲ ရောက်သွားပါတယ်။ Provider အချို့က query ထဲ key ကို လက်ခံပေမယ့် header (Authorization ဒါမှမဟုတ် provider သတ်မှတ်တဲ့ header) ကို ရွေးလို့ရရင် header ကို သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • URL ကို new URL(path, base) နဲ့၊ query ကို searchParams နဲ့ တည်ဆောက်ပါ၊ ကိုယ်တိုင် encode လုပ်စရာ မလိုပါဘူး။
  • Base URL ကို config တစ်နေရာထဲမှာ ထားပါ၊ code တစ်လျှောက် hard-code မလုပ်ပါနဲ့။
  • Accept ကို ပွင့်ပွင့်လင်းလင်း ပို့ပါ၊ server က format ကို ရွေးစရာ ရှိရင် အတိအကျ တောင်းလို့ရပါတယ်။
  • Log ထဲမှာ URL ကို print ထုတ်တဲ့အခါ query ထဲက secret ကို ဖျောက်ပါ၊ Authorization header တန်ဖိုးကို လုံးဝ မရေးပါနဲ့။

Mini challenge

Mini challenge: buildUrl helper

base URL၊ path နဲ့ params object ကို လက်ခံပြီး URL အပြည့်အစုံကို string အဖြစ် ပြန်ပေးတဲ့ buildUrl() ကို ရေးပါ။ Encode လုပ်တာကို URLSearchParams ကို လုပ်ခိုင်းပါ။

  • base နဲ့ path ကြားမှာ slash တစ်ခုတည်း ရှိရမယ် (base မှာ / ပါလည်း မပါလည်း၊ path မှာ / နဲ့ စလည်း မစလည်း ရမယ်)
  • တန်ဖိုးက undefined ဒါမှမဟုတ် null ဖြစ်တဲ့ param ကို ကျော်ရမယ်
  • Array တန်ဖိုးတွေအတွက် နာမည်ကို ထပ်ရေးရမယ် (tag=a&tag=b)
  • တန်ဖိုး (နံပါတ်လည်း ရမယ်) တိုင်းကို string ပြောင်းပြီး ပြီးတော့ ပေးထားတဲ့ ဥပမာ သုံးခုကို print ထုတ်ရမယ်
build-url.mjs
function buildUrl(base, path, params = {}) {
  // TODO: join base and path with exactly one slash between them
  // TODO: skip params whose value is undefined or null
  // TODO: repeat the name for array values (tag=a&tag=b)
  // TODO: let URLSearchParams do all the encoding
  // TODO: return the final URL as a string
}

// TODO: print three URLs: a search with q, page and tags, a plain path,
//       and a note whose text contains "#"

ရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: https://api.example.com/v1/search?q=fish+%26+chips&page=2&tag=a&tag=b https://api.example.com/v1/users/42 https://api.example.com/v1/notes?note=item+%233

အရိပ်အမြွက် ကြည့်ရန်

new URL(path.replace(/^\/+/, ""), base.endsWith("/") ? base : base + "/") နဲ့ စပါ။ ပြီးရင် Object.entries(params) ကို loop ပတ်ပြီး Array ဖြစ်မဖြစ် စစ်ကာ url.searchParams.append(name, String(item)) လုပ်ပါ။ နောက်ဆုံး url.toString() ပြန်ပါ။

အဖြေ ကြည့်ရန်
build-url.mjs
function buildUrl(base, path, params = {}) {
  const root = base.endsWith("/") ? base : base + "/";
  const url = new URL(path.replace(/^\/+/, ""), root);
  for (const [name, value] of Object.entries(params)) {
    if (value === undefined || value === null) continue;
    for (const item of Array.isArray(value) ? value : [value]) {
      url.searchParams.append(name, String(item));
    }
  }
  return url.toString();
}

const host = "https://api.example.com";
const search = { q: "fish & chips", page: 2, tag: ["a", "b"], skip: undefined };
console.log(buildUrl(host, "/v1/search", search));
console.log(buildUrl(host + "/", "v1/users/42"));
console.log(buildUrl(host, "/v1/notes", { note: "item #3" }));

Quick Quiz 1 / 3

Client က https://api.example.com/v1/users?page=2#top ကို တောင်းရင် server ဆီ ဘာ ရောက်မလဲ။

Quick Quiz 2 / 3

Header မပါဘဲ fetch(url, { method: "POST", body: JSON.stringify({ a: 1 }) }) ကို ပို့ရင် server က Content-Type ကို ဘာလို့ မြင်မလဲ။

Quick Quiz 3 / 3

fish & chips ဆိုတဲ့ တန်ဖိုးအတွက် query string ကို ဘေးကင်းစွာ ဘယ်လို ဆောက်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • URL မှာ scheme, host, port, path, query, fragment ပါပြီး fragment ကို server ဆီ ဘယ်တော့မှ မပို့ပါဘူး။ Identity ကို path မှာ၊ option တွေကို query မှာ ထားပါ။
  • Query ကို URLSearchParams နဲ့ တည်ဆောက်ပါ၊ တန်ဖိုးတစ်ခုကို encodeURIComponent နဲ့ encode လုပ်ပါ၊ နှစ်ခါ မ encode ပါနဲ့။
  • Accept က လက်ခံမယ့် format၊ Content-Type က ပို့မယ့် body ရဲ့ format၊ Authorization က credential ပါ။
  • JSON ပို့ရင် Content-Type: application/json ကို အမြဲထည့်ပါ၊ fetch ရဲ့ default က text/plain ပါ။ Secret တွေကို URL ထဲ မထည့်ပါနဲ့။

RFC 9110 — HTTP Semantics — IETF

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Endpoint ပုံစံ (URL/Params/Headers/Body) | Thuta Learning