HTTP Request နဲ့ Fetch

ECMAScript 2026

HTTP ဆိုတာ request နဲ့ response အပြန်အလှန် ပို့ဖို့ စည်းမျဉ်းအစုပါ။ Request မှာ လိပ်စာနဲ့ method ရေးပါတယ်။ Response မှာ အခြေအနေဖော်ပြတဲ့ status code ပါပြီး data တင်ပို့တဲ့ body လည်း ပါနိုင်ပါတယ်။ API က program တစ်ခုက service ဆီ data သို့မဟုတ် လုပ်ဆောင်ချက် တောင်းဖို့ သတ်မှတ်ထားတဲ့ နည်းလမ်းပါ။

ဒီစာမျက်နှာမှာ

JSON ဆိုတာဘာလဲ။

JSON ဆိုတာ data ဖလှယ်ဖို့ သုံးတဲ့ စာသားပုံစံတစ်ခုပါ။ JSON object ရဲ့ property နာမည်နဲ့ string တန်ဖိုးတွေကို double quotes နဲ့ ရေးပါတယ်။ JSON.parse က JSON စာသားကို JavaScript တန်ဖိုးအဖြစ် ဖတ်ပြီး JSON.stringify က တန်ဖိုးကို JSON စာသားအဖြစ် ထုတ်ပေးပါတယ်။

စမ်းသပ်နည်း ဒီနမူနာတွေအတွက် folder အသစ်လုပ်ပြီး code ကို product.json နဲ့ သိမ်းပါ။

{ "name": "Pen", "price": 2500 }

စမ်းကြည့်ရင် ဘာဖြစ်မလဲ GET နမူနာမှာ သုံးမယ့် JSON ဖိုင်ရပါပြီ။

GET Request နဲ့ Data ဖတ်ခြင်း

GET က အချက်အလက်တစ်ခုကို တောင်းဖတ်တဲ့ method ပါ။ fetch() က Response ရမယ့် promise ပြန်ပေးပါတယ်။ Status code 200 ကနေ 299 ထိဆို response.ok က true ပါ။ Server က 404 ပြန်ရင်လည်း fetch promise မှာ Response ရနိုင်သေးလို့ body မဖတ်ခင် ok စစ်ပါ။ response.json() က body ကို JSON အဖြစ် ဖတ်ပြီး ရလဒ် promise ပြန်ပေးပါတယ်။

စမ်းသပ်နည်း product.json ဘေးမှာ index.html နဲ့ သိမ်းပါ။ VS Code Live Server သုံးပါ။ ဒါမှမဟုတ် အဲဒီ folder ထဲ python -m http.server 8000 run ပြီး http://localhost:8000 ဖွင့်ပါ။

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>GET demo</title>
</head>
<body>
  <p id="result">Loading…</p>
  <script>
    async function loadProduct() {
      const result = document.querySelector("#result");
      try {
        const response = await fetch("./product.json");
        if (!response.ok) throw new Error("HTTP " + response.status);
        const product = await response.json();
        result.textContent = product.name + ": " + product.price;
      } catch (error) {
        result.textContent = "Could not load the product: " + error.message;
      }
    }
    loadProduct();
  </script>
</body>
</html>

စမ်းကြည့်ရင် ဘာဖြစ်မလဲ စာမျက်နှာမှာ Pen: 2500 ပေါ်ပါမယ်။ Request ထဲက product.json ကို missing.json လို့ ပြောင်းပြီး reload လုပ်ရင် message မှာ HTTP 404 ပါတာ ကြည့်ပါ။

ရလဒ်ကွာနေရင် လိပ်စာက file: နဲ့ စရင် local server URL နဲ့ ဖွင့်ပါ။ 404 ဆိုရင် ဖိုင်နာမည်နဲ့ server ဖွင့်ထားတဲ့ folder ကို စစ်ပါ။

POST Request နဲ့ JSON ပို့ခြင်း

POST က server ကို အလုပ်လုပ်ဖို့ data ပို့တဲ့ method ပါ။ ဒီမှာ method ကို POST သတ်မှတ်ထားပါတယ်။ Content-Type header က body ပုံစံ ပြောပြီး JSON.stringify က data ကို အဲဒီစာသားပုံစံ ပြောင်းပေးပါတယ်။ ဒီနမူနာ API က ဖန်တီးပြီးသလို စမ်းသပ်ရလဒ် ပြန်ပေးပါတယ်။

စမ်းသပ်နည်း GET နမူနာ folder ထဲ post.html နဲ့ သိမ်းပါ။ Internet ချိတ်ထားပြီး အရင် local server ကနေ ဖွင့်ပါ။ Send example post ခလုတ် နှိပ်ပါ။

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>POST demo</title>
</head>
<body>
  <button id="submit" type="button">Send example post</button>
  <p id="result" role="status"></p>
  <script>
    const button = document.querySelector("#submit");
    const result = document.querySelector("#result");
    button.addEventListener("click", async () => {
      result.textContent = "Sending…";
      try {
        const response = await fetch("https://jsonplaceholder.typicode.com/posts", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ title: "Pen", body: "A sample product post", userId: 1 }),
        });
        if (!response.ok) throw new Error("HTTP " + response.status);
        const data = await response.json();
        result.textContent = "Received: " + data.title + " (ID " + data.id + ")";
      } catch (error) {
        result.textContent = "Could not send: " + error.message;
      }
    });
  </script>
</body>
</html>

စမ်းကြည့်ရင် ဘာဖြစ်မလဲ Demo API က title နဲ့ စမ်းသပ် ID အသစ် ပြန်ပေးပါတယ်။ Service က သတ်မှတ်ထားတဲ့အတိုင်း ပြန်ရင် “Received: Pen (ID 101)” ပေါ်ပါမယ်။

တရားဝင် ကိုးကားချက်များ