ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
API ကို code ရေးမစခင် လက်နဲ့ အရင် စမ်းကြည့်ချင်ပါတယ်၊ စက်ကို မဖွင့်ခင် စက်ပြင်သမားက အသံနားထောင်သလိုပါပဲ။ Request တစ်ခုကို ပို့ပြီး အဖြေအစစ်ကို မျက်မြင်ပြတဲ့ ကိရိယာက ဒီ stethoscope ပါ။ curl က စာကြောင်းတစ်ကြောင်းနဲ့ terminal ကနေ လုပ်ပေးပါတယ်။ Postman နဲ့ Insomnia ကတော့ form နဲ့ button တွေနဲ့ လုပ်ပေးပြီး history ကိုလည်း သိမ်းပေးပါတယ်။
အားလုံးက နောက်ပိုင်း ရေးမယ့် fetch() ခေါ်ဆိုမှုနဲ့ အလုပ်တူပါတယ် — request တည်ဆောက်၊ ပို့၊ response ပြ။ Request က curl မှာ အလုပ်လုပ်ပြီး ကိုယ့် code မှာ မလုပ်ရင် ကွာတာက request ထဲမှာ ရှိပြီး နှစ်ခုကို နှိုင်းယှဉ်ရင် ရှာတွေ့နိုင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: ဘာပို့လိုက်လဲ မမြင်ရတော့ debug လုပ်လို့ မရ
API documentation ထဲက ဥပမာတစ်ခုကို ကိုယ့် app ထဲ ကူးထည့်လိုက်တာ 400 Bad Request ပြန်လာပါတယ်။ Error က အသုံးဝင်တာ ဘာမှ မပြောပါဘူး။ URL မှားလား၊ header ကျန်လား၊ JSON ပျက်နေလား၊ ဒါမှမဟုတ် ကိုယ့် code က request ကို တစ်နည်းနည်းနဲ့ ပြောင်းလိုက်လား။ ဘာပို့လိုက်လဲ အတိအကျ မမြင်ရရင် မျက်စိစုံမှိတ် debug လုပ်နေရတာပါ။
ဒီပြဿနာရဲ့ အဖြစ်များတဲ့ ပုံစံက editor ထဲမှာ မှန်သလိုမြင်ရတဲ့ body က server ဆီ Content-Type မှားပြီး ရောက်တာပါ။ Server က တိုင်တန်းတဲ့အချိန်မှာ ဘယ်အပိုင်းကို အရင်စစ်ရမလဲ မသိတော့ပါဘူး။ ဖြေရှင်းနည်းက ကိုယ့် app ကို ဖယ်ထုတ်ပြီး flag တိုင်းကို နားလည်တဲ့ ကိရိယာတစ်ခုကနေ အတူတူ request ကို ပို့ကြည့်တာပါ။
# Illustrative: api.example.com is a placeholder host
curl -X POST https://api.example.com/v1/users -d '{"name":"Alice"}'
# With -d and no Content-Type header, curl labels the body
# application/x-www-form-urlencoded, so a JSON-only API may answer
# 415 Unsupported Media Type or 400 Bad Request.ကိရိယာက ဘာကြောင့် ကူညီပေးတာလဲ
ကိရိယာက request ကို method၊ URL၊ header၊ body ဆိုတဲ့ ထင်ရှားတဲ့ အပိုင်းတွေအဖြစ် ပြပါတယ်။ အပိုင်းတိုင်း မြင်ရရင် တစ်ကြိမ်မှာ တစ်ခုပဲ ပြောင်းပြီး ဘယ်ပြောင်းလဲမှုက အဖြေကို ပြင်လဲ ကြည့်လို့ရပါတယ်။ ဒါက ဆရာဝန်က အပူချိန်တိုင်းတာ ကိရိယာသုံးတဲ့ နည်းနဲ့ အတူတူပါပဲ။ Curl မှာ အလုပ်လုပ်ပြီး ကိုယ့် code မှာ မလုပ်တဲ့ request တစ်ခုက server မှာ ပြဿနာ မရှိဘူးဆိုတာ သက်သေပြပြီး ပြဿနာကို ကိုယ့် code ဆီ ညွှန်ပြပါတယ်။
Core concept: curl၊ GUI client နဲ့ DevTools
curl ဆိုတာ URL တွေနဲ့ data လွှဲပြောင်းတဲ့ command-line program ပါ။ macOS နဲ့ Linux distribution အများစုမှာ ပါပြီးသားဖြစ်ပြီး Windows 10 နဲ့ 11 မှာလည်း curl.exe အနေနဲ့ ပါပါတယ်။ အားသာချက်က ဘာမှ မဖုံးထားတာပါ — request ရဲ့ အပိုင်းတိုင်းက flag တစ်ခုပါ။ -X က method ရွေးတယ်၊ -H က header ထည့်တယ်၊ -d က request body သတ်မှတ်တယ်၊ URL က နောက်ဆုံးမှာ ရှိပါတယ်။ Install ဖြစ်မဖြစ် curl --version နဲ့ စစ်ကြည့်ပါ။
Default အနေနဲ့ curl က response body ကိုပဲ print ထုတ်ပါတယ်။ ကျန်တာတွေ မြင်ချင်ရင် -i ထည့်ပါ၊ body မတိုင်ခင် status line နဲ့ response header တွေကို ပြပါလိမ့်မယ်။ -v ကတော့ curl ပို့တဲ့ request header တွေအပါအဝင် ဆက်သွယ်မှုအားလုံးကို ပြပါတယ် (> နဲ့ စတဲ့ လိုင်းတွေက ပို့တာ၊ < နဲ့ စတာတွေက လက်ခံတာ)။ -s က progress meter ကို ပိတ်တယ်၊ -o file က body ကို file ထဲ သိမ်းတယ်၊ -w "%{http_code}" က status code ကိုပဲ print ထုတ်တယ်။ Server ပျက်နေလို့ အဆုံးမရှိ စောင့်နေမှာ မဟုတ်အောင် --max-time 10 ထည့်ပါ၊ HTTP error status မှာ curl က exit code သုံးနိုင်ဖို့ -f (--fail) ထည့်ပါ၊ script တွေမှာ ဒါက အရေးကြီးပါတယ်။
JSON ပို့တာက beginner တွေ ခလုတ်တိုက်တတ်တဲ့ နေရာပါ။ -d က string ကို body အဖြစ် ပို့ပေမယ့် -H "Content-Type: application/json" မထည့်ရင် application/x-www-form-urlencoded လို့ တံဆိပ်တပ်ပါတယ်။ Quote ထည့်ပုံက နောက်ထပ် ထောင်ချောက်ပါ။ macOS နဲ့ Linux မှာ JSON ကို single quote နဲ့ ခတ်ပါ။ Windows cmd.exe မှာတော့ single quote က quote အဖြစ် အလုပ်မလုပ်ပါဘူး၊ double quote နဲ့ ခတ်ပြီး အတွင်းက double quote တွေကို \" နဲ့ escape လုပ်ပါ။ PowerShell 5.1 မှာ curl.exe လို့ ရိုက်ပါ၊ curl ကို ရိုက်ရင် Invoke-WebRequest ရဲ့ alias ဖြစ်နေလို့ပါ။ ဘယ် shell မှာမဆို အလုပ်လုပ်တဲ့ နည်းက JSON ကို file ထဲ ထည့်ပြီး -d @body.json သုံးတာပါ။
GUI client တွေဖြစ်တဲ့ Postman နဲ့ Insomnia က curl နဲ့ မတူတဲ့ ပြဿနာတွေကို ဖြေရှင်းပါတယ်။ Request တွေကို collection အဖြစ် သိမ်းနိုင်တယ်၊ environment variable တွေ သတ်မှတ်နိုင်တယ် (staging အတွက် base URL တစ်ခု၊ production အတွက် တစ်ခု)၊ JSON ကို format လုပ်ပြတယ်၊ request အဆက်မပြတ်ကို run နိုင်တယ်၊ Postman မှာဆိုရင် response ကို အလိုအလျောက် စစ်တဲ့ test script ရေးနိုင်ပါတယ်။ Bug report ဒါမှမဟုတ် script ထဲ paste လုပ်လို့ရတဲ့ command လိုချင်ရင် curl ကို သုံးပါ။ မသိတဲ့ API ကို စူးစမ်းပြီး history နဲ့ ဖွဲ့စည်းပုံ လိုချင်ရင် GUI client ကို သုံးပါ။ ကိုယ့် website က ခေါ်နေတဲ့ API တွေအတွက် browser DevTools ရဲ့ Network tab က တတိယနည်းပါ — request တစ်ခုပေါ် right-click နှိပ်ပြီး cURL အဖြစ် copy လုပ်လို့ရပါတယ်။
ဘယ်ကိရိယာကို ရွေးရွေး သိမ်းမယ့် ဒါမှမဟုတ် မျှဝေမယ့် request ထဲမှာ token အစစ်ကို မကူးထည့်ပါနဲ့။ Variable တွေကို သုံးပါ — shell ထဲမှာ secret ကို export လုပ်ပြီး curl မှာ ကိုးကားပါ (-H "Authorization: Bearer $API_TOKEN")၊ ဒါမှမဟုတ် Postman environment variable ထဲ ထည့်ပြီး {{token}} လို့ ကိုးကားပါ။ Secret ပါတဲ့ collection ကို export လုပ်လိုက်တာက secret ပေါက်ကြားတာပါပဲ။
- Request ကို လက်နဲ့ ရေးတယ်
- curl ဒါမှမဟုတ် Postman နဲ့ ပို့တယ်
- Status, header, body ကို ဖတ်တယ်
- တစ်ခုပြောင်းပြီး ပြန်ပို့တယ်
တစ်ကြိမ်မှာ တစ်ခုပဲ ပြောင်းပါ၊ ဒါမှ ဘာက ပြဿနာကို ပြင်လဲ သိပါမယ်။
အထွေထွေ ပုံစံ: curl command
curl [options] URL
# -X METHOD choose the method (GET is the default)
# -H "Name: value" add a header (repeat it for more headers)
# -d 'body' | -d @file send a request body (implies POST)
# -i | -v show response headers | show the whole exchange
# -s -o file quiet mode | save the body to a file
# -w "%{http_code}" print the status code after the response
# --max-time N -f give up after N seconds | fail on HTTP errorsOption တွေက URL ရဲ့ အရှေ့မှာ ဖြစ်ဖြစ် နောက်မှာ ဖြစ်ဖြစ် ရပါတယ်။ -H ကို header တစ်ခုစီအတွက် ထပ်ရေးပါ။ -d ကို သုံးရင် method က POST ဖြစ်သွားလို့ -X POST ကို ထည့်စရာ မလိုပေမယ့် ထည့်ထားရင် ဖတ်ရလွယ်ပါတယ်။ URL ကို အမြဲ quote နဲ့ ခတ်ပါ၊ & လို အက္ခရာတွေကို shell က ကိုယ်တိုင် ဆက်ကိုင်မိမှာ စိုးရိမ်ရလို့ပါ။
Example: ပို့တာ အတိအကျ ပြန်ပြတဲ့ echo server
Echo server ဆိုတာ လက်ခံရတဲ့ request ကို ဖော်ပြတဲ့ response ပြန်ပေးတဲ့ server ပါ — ကိုယ့်ကိရိယာ ဘာတွေ ပို့လဲ စစ်ဖို့ အကောင်းဆုံး debug ကိရိယာပါ။ ဒီ script က echo server တစ်ခု စတင်ပြီး curl command တစ်ခုနဲ့ ညီမျှတဲ့ fetch request ကို ပို့ပါတယ်။ Server မြင်တဲ့ method၊ URL (query ပါ)၊ header နဲ့ body ကို ပြန်ပြပါလိမ့်မယ်။ Authorization header အတွက်တော့ တန်ဖိုးကို ပြန်မပြဘဲ ပါမပါပဲ ပြပါတယ်၊ secret ကို ပြန်ရေးမထုတ်တဲ့ အလေ့အကျင့်ကောင်းပါ။
import http from "node:http";
// An echo server: it answers with a description of the request it received.
const server = http.createServer((req, res) => {
let raw = "";
req.on("data", (chunk) => (raw += chunk));
req.on("end", () => {
const seen = {
method: req.method,
url: req.url,
accept: req.headers["accept"],
contentType: req.headers["content-type"],
authorization: req.headers["authorization"] ? "(present)" : "(none)",
body: raw,
};
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(seen, null, 2));
});
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = `http://127.0.0.1:${server.address().port}`;
// The same request as:
// curl -X POST "$BASE/echo?debug=1" -H "Accept: application/json" \
// -H "Content-Type: application/json" -d '{"name":"Alice"}'
const res = await fetch(`${base}/echo?debug=1`, {
method: "POST",
headers: { Accept: "application/json", "Content-Type": "application/json" },
body: JSON.stringify({ name: "Alice" }),
});
console.log(res.status);
console.log(await res.text());
server.close();Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lower-case header names
contentType: req.headers["content-type"],Node က request header နာမည်တွေကို စာလုံးသေးအဖြစ် ပေးပါတယ်၊ client က Content-Type လို့ ပို့ပို့ content-type လို့ ပို့ပို့ ပုံစံတူပါပဲ။ HTTP header နာမည်တွေက စာလုံးအကြီးအသေး မခွဲပါဘူး။
- Never echo secrets
authorization: req.headers["authorization"] ? "(present)" : "(none)",Authorization ရဲ့ တန်ဖိုးကို ပြန်မထုတ်ဘဲ ရှိမရှိပဲ ပြပါတယ်။ Debug output တွေက log ထဲ ရောက်တတ်လို့ token ကို ရေးထုတ်မိရင် ပေါက်ကြားနိုင်ပါတယ်။
- fetch(url, options)
const res = await fetch(`${base}/echo?debug=1`, {fetch ရဲ့ ဒုတိယ argument ဖြစ်တဲ့ options object က curl ရဲ့ -X, -H, -d flag တွေနဲ့ ညီမျှပါတယ်။ ?debug=1 ကတော့ URL ထဲက query string ပါ။
- Headers
headers: { Accept: "application/json", "Content-Type": "application/json" },Accept က ကိုယ်လက်ခံနိုင်တဲ့ format ကို ပြောပြီး Content-Type က ပို့လိုက်တဲ့ body ရဲ့ format ကို ပြောပါတယ်။ ဒုတိယ key မှာ hyphen ပါလို့ quote ခတ်ထားရတာပါ။
- Body is a string
body: JSON.stringify({ name: "Alice" }),fetch body က string (ဒါမှမဟုတ် binary) ဖြစ်ရပါမယ်၊ object တိုက်ရိုက် ပို့လို့ မရပါဘူး။ ဒါကြောင့် JSON.stringify နဲ့ ပြောင်းပါတယ်။ ဒါက curl ရဲ့ -d '...' နဲ့ ညီမျှပါတယ်။
- Read as text
console.log(await res.text());res.text() က body ကို မပြောင်းလဲဘဲ စာသားအဖြစ် ပေးပါတယ်၊ ဒါကြောင့် server ပို့တဲ့ အတိုင်း အတိအကျ မြင်ရပါတယ်။ Debug လုပ်ချိန်မှာ res.json() ထက် res.text() က ပိုကောင်းလေ့ရှိပါတယ်၊ JSON မဟုတ်တဲ့ အဖြေလာရင်လည်း မပျက်လို့ပါ။
200
{
"method": "POST",
"url": "/echo?debug=1",
"accept": "application/json",
"contentType": "application/json",
"authorization": "(none)",
"body": "{\"name\":\"Alice\"}"
}တူညီတဲ့ request ကို curl နဲ့ ရေးတာ
အပေါ်က fetch နဲ့ ညီမျှတဲ့ curl command ပါ။ api.example.com က ဥပမာအတွက် သတ်မှတ်ထားတဲ့ အတု host ပါ။ -i ထည့်ထားလို့ response ရဲ့ status line နဲ့ header တွေကိုပါ မြင်ရပါမယ်။
curl -i -X POST "https://api.example.com/v1/users?debug=1" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{"name":"Alice"}'curl က ပို့တဲ့အရာကို wire ပေါ်မှာ ဒီလို စာသားပုံစံ ဖြစ်ပါတယ် (Content-Length က body စာလုံး ၁၆ လုံးကို ဖော်ပြပြီး User-Agent ရဲ့ version က စက်အလိုက် ကွာပါမယ်)။ Request line၊ header၊ blank line တစ်ကြောင်း၊ ပြီးမှ body ဆိုတဲ့ အစီအစဉ်ကို မှတ်ထားပါ။
POST /v1/users?debug=1 HTTP/1.1
Host: api.example.com
User-Agent: curl/8.18.0
Accept: application/json
Content-Type: application/json
Content-Length: 16
{"name":"Alice"}Shell အလိုက် quote ပုံစံ ကွာတယ်
ဒီ course ရဲ့ curl ဥပမာတွေက macOS၊ Linux နဲ့ Git Bash အတွက် ရေးထားတာပါ။ Windows cmd.exe မှာ JSON ကို -d "{\"name\":\"Alice\"}" လို့ ရေးပါ၊ single quote ကိုသုံးရင် single quote တွေကို body ထဲ စာလုံးအဖြစ်ပါ ပို့မိပါလိမ့်မယ်။ PowerShell 5.1 မှာ curl.exe ကို သုံးပြီး -d '{\"name\":\"Alice\"}' လို့ ရေးပါ။ environment variable ကိုလည်း cmd မှာ %API_TOKEN%၊ PowerShell မှာ $env:API_TOKEN နဲ့ ကိုးကားပါတယ်။ ဘယ် shell မှာမဆို မမှားချင်ရင် JSON ကို file ထဲ ထည့်ပြီး -d @body.json သုံးပါ။
curl နဲ့ GUI client ကို ရွေးချယ်ခြင်း
| လုပ်ငန်း | curl | Postman / Insomnia |
|---|---|---|
| Bug report ထဲ ထည့်တာ | စာကြောင်းတစ်ကြောင်း copy လုပ်ရုံ | Request ကို export ဒါမှမဟုတ် share လုပ် |
| မသိတဲ့ API ကို စူးစမ်းတာ | လက်နဲ့ ရိုက်ရ | Collection၊ history၊ ပုံစံချ JSON |
| Automated စစ်ဆေးမှု | Shell script (curl -f) | Test script နဲ့ collection runner |
| Secret ထိန်းသိမ်းတာ | Shell environment variable | App ထဲက environment variable |
နှစ်မျိုးလုံး သင်ယူသင့်ပါတယ်။ Documentation အားလုံးလိုလိုက curl ဥပမာတွေ ပေးတာမို့ curl ကို ဖတ်တတ်ဖို့ လိုပြီး GUI client က အဖွဲ့လိုက် အလုပ်လုပ်တဲ့အခါ အားသာပါတယ်။ Product အသေးစိတ် (menu နာမည်တွေ၊ feature တွေ) က version အလိုက် ပြောင်းလဲတတ်လို့ ကိုယ်သုံးတဲ့ version ရဲ့ official guide ကို ကြည့်ပါ။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဒီကိရိယာတွေကို ဘယ်လို သုံးလဲ
Production bug တစ်ခုကို လူတစ်ယောက်က အစီရင်ခံတဲ့အခါ အကောင်းဆုံး ပြန်ထုတ်ကြည့်နည်းက log ထဲက request ကို curl command အဖြစ် ပြောင်းပြီး ပြန်ပို့တာပါ။ Team တွေက Postman collection ကို အသက်ရှင်နေတဲ့ documentation အဖြစ် မျှဝေကြပြီး လူသစ်တစ်ယောက်က ဘာကိုမှ ရိုက်စရာမလိုဘဲ API ကို စမ်းကြည့်နိုင်ပါတယ်။ Browser DevTools က ကိုယ့် web app ဘာ API တွေ ခေါ်နေလဲ တိတိကျကျ ပြပေးပါတယ်။ CI pipeline တွေကလည်း deploy ပြီးတာနဲ့ traffic အစစ်ကို version အသစ်ဆီ မပို့ခင် curl နဲ့ smoke test တိုတိုလေး run လေ့ရှိပါတယ်။
Deploy script တွေနဲ့ monitoring ကလည်း curl ကို အကြိမ်ကြိမ် သုံးပါတယ်။ အောက်က command က service ကျန်းမာလား စစ်ပြီး မကျန်းမာရင် exit code ကို သုံးနိုင်ပါတယ် — -f က 4xx နဲ့ 5xx မှာ ကျရှုံးစေပြီး -sS က progress ကို ဖျောက်ပေမယ့် error message ကိုတော့ ပြပါတယ်။ ဒီ command မှာလည်း api.example.com က ဥပမာ host ပါ။
# Illustrative health check for a deploy script (placeholder host)
curl -fsS --max-time 5 https://api.example.com/health || echo "service is down"အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. -d နဲ့ Content-Type မထည့်တာ
curl -d ကို Content-Type မပါဘဲ သုံးရင် body က application/x-www-form-urlencoded အဖြစ် ရောက်ပါတယ်။ JSON ကိုပဲ လက်ခံတဲ့ API က 415 ဒါမှမဟုတ် 400 နဲ့ ငြင်းပါလိမ့်မယ်။ ကိုယ့် JSON မှန်နေလို့ မျက်စိကျေနပ်ပေမယ့် server က မဖတ်တတ်တာပါ။ -H "Content-Type: application/json" ကို အမြဲ ထည့်ပါ။
2. URL ကို quote မခတ်တာ
bash မှာ curl https://api.example.com/v1/items?page=1&limit=5 လို့ ရိုက်ရင် & ကို shell က "command ကို နောက်ကွယ်မှာ run" လို့ ဖတ်ပါတယ်၊ ဒါကြောင့် limit=5 ပျောက်သွားပါတယ်။ URL တစ်ခုလုံးကို double quote နဲ့ ခတ်ပါ။ Windows cmd မှာတော့ single quote က အလုပ်မလုပ်လို့ double quote ကိုပဲ သုံးပါ။
3. Token အစစ်ကို command ထဲ တိုက်ရိုက် ရိုက်တာ
curl -H "Authorization: Bearer sk-..." လို့ ရိုက်ရင် token က shell history file ထဲ ရောက်သွားပြီး screen share ဒါမှမဟုတ် bug report ထဲလည်း ပါသွားနိုင်ပါတယ်။ Token ကို environment variable ထဲ ထည့်ပြီး $API_TOKEN လို့ ကိုးကားပါ၊ Postman မှာလည်း environment variable သုံးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Debug လုပ်ချိန်မှာ -i (ဒါမှမဟုတ် -v) ကို အမြဲ ထည့်ပါ၊ status နဲ့ header ကို မမြင်ရရင် ပြဿနာရဲ့ တစ်ဝက်ကို မမြင်ရတာပါ။
- Script ထဲမှာ --max-time နဲ့ -f ကို သုံးပါ၊ ဒါမှ ပျက်နေတဲ့ server က script ကို မရပ်စေပါဘူး။
- JSON body အရှည်တွေကို file တစ်ခုထဲ သိမ်းပြီး -d @file နဲ့ သုံးပါ၊ quote ပြဿနာ ကင်းပြီး version control ထဲ ထည့်လို့လည်း ရပါတယ်။
- အလုပ်ဖြစ်တဲ့ request တွေကို collection ထဲ ဒါမှမဟုတ် script file ထဲ သိမ်းထားပါ၊ နောက်တစ်ခါ ပြန်သုံးလို့ရပါတယ်။
Mini challenge
Mini challenge: request ကနေ curl command ထုတ်ပေးမယ့် function
{ method, url, headers, body } object ကို လက်ခံပြီး macOS/Linux shell မှာ run လို့ရတဲ့ curl command string တစ်ကြောင်း ပြန်ပေးတဲ့ toCurl() function ကို ရေးပါ။
- Method က GET (default) ဆိုရင် -X ကို မထည့်ရ၊ တခြားဆိုရင် -X METHOD ထည့်ရမယ်
- URL နဲ့ အပိုင်းတိုင်းကို single quote နဲ့ ခတ်ရမယ်
- Header တစ်ခုစီအတွက် -H 'Name: value' တစ်ခုစီ ထည့်ရမယ်
- body ရှိရင် -d '<JSON.stringify(body)>' ထည့်ရမယ်၊ တန်ဖိုးထဲက single quote ကို '\'' အဖြစ် escape လုပ်ရမယ်
// A value wrapped in single quotes for a POSIX shell
function quote(text) {
// TODO: wrap in ' ... ' and turn every ' inside into '\''
}
function toCurl({ method = "GET", url, headers = {}, body }) {
// TODO: start with ["curl"]
// TODO: add -X METHOD unless the method is GET
// TODO: add the quoted URL
// TODO: add -H for every header
// TODO: add -d with the JSON body when there is one
// TODO: join the parts with spaces
}
// TODO: print toCurl for a plain GET and for a POST with headers and a bodyရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ် (ဒုတိယ command က တစ်ကြောင်းတည်းပါ): curl 'https://api.example.com/v1/health' curl -X POST 'https://api.example.com/v1/users' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"name":"Alice","nickname":"Ma'\''am"}'
အရိပ်အမြွက် ကြည့်ရန်
Single quote ကို escape လုပ်ဖို့ String(text).replaceAll("'", "'\\''") ကို သုံးပါ — shell မှာ ' ကို ပိတ်ပြီး \' ထည့်ကာ ' ပြန်ဖွင့်တာပါ။ Part တွေကို array ထဲ push လုပ်ပြီး နောက်ဆုံး join(" ") လုပ်ပါ။
အဖြေ ကြည့်ရန်
// A value wrapped in single quotes for a POSIX shell
function quote(text) {
return "'" + String(text).replaceAll("'", "'\\''") + "'";
}
function toCurl({ method = "GET", url, headers = {}, body }) {
const parts = ["curl"];
if (method !== "GET") parts.push("-X", method);
parts.push(quote(url));
for (const [name, value] of Object.entries(headers)) {
parts.push("-H", quote(`${name}: ${value}`));
}
if (body !== undefined) parts.push("-d", quote(JSON.stringify(body)));
return parts.join(" ");
}
console.log(toCurl({ url: "https://api.example.com/v1/health" }));
console.log(
toCurl({
method: "POST",
url: "https://api.example.com/v1/users",
headers: { "Content-Type": "application/json", Accept: "application/json" },
body: { name: "Alice", nickname: "Ma'am" },
})
);Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- curl နဲ့ GUI client တွေက request ကို မြင်သာအောင် ပြပေးတဲ့ ကိရိယာတွေပါ၊ curl မှာ အလုပ်လုပ်ပြီး ကိုယ့် code မှာ မလုပ်ရင် ပြဿနာက ကိုယ့် request ထဲမှာပါ။
- curl ရဲ့ အဓိက flag တွေက -X, -H, -d, -i, -v, -s, -o, -w, --max-time, -f ဖြစ်ပြီး JSON ပို့ရင် Content-Type: application/json ကို မမေ့ပါနဲ့။
- Quote ပုံစံက shell အလိုက် ကွာပါတယ် — cmd မှာ double quote နဲ့ \" ၊ PowerShell 5.1 မှာ curl.exe၊ ဒါမှမဟုတ် -d @body.json နဲ့ ရှောင်ပါ။
- Token အစစ်ကို command၊ collection၊ URL ထဲ မထည့်ဘဲ environment variable နဲ့ ကိုးကားပါ။
RFC 9110 — HTTP Semantics — IETF