กลับไปหน้าสูตร
#curl#jq#api#cheatsheet
curl + jq Cheatsheet
รวมคำสั่ง curl และ jq แบบจัดเต็มสำหรับทดสอบ API, ส่ง auth, parse JSON, filter data และ debugging
11 มีนาคม 2569อ่านประมาณ 1 นาที
สารบัญสูตร
curl Basics
| Command | ใช้ทำอะไร |
|---|---|
curl -I https://example.com | ดู headers |
curl -L https://example.com | follow redirect |
curl -sS https://example.com | silent + show errors |
curl -o out.json URL | save output file |
curl -w "\n%{http_code}\n" URL | แสดง status code |
HTTP Methods
curl -X GET https://api.example.com/users
curl -X POST https://api.example.com/users -H 'content-type: application/json' -d '{"name":"Olan"}'
curl -X PUT https://api.example.com/users/1 -H 'content-type: application/json' -d '{"name":"New"}'
curl -X DELETE https://api.example.com/users/1
Auth Patterns
| Pattern | ตัวอย่าง |
|---|---|
| Bearer token | -H "authorization: Bearer $TOKEN" |
| Basic auth | -u user:pass |
| API key header | -H "x-api-key: $API_KEY" |
| Cookie | -H "cookie: sid=..." |
jq Basics
| Command | ใช้ทำอะไร |
|---|---|
jq '.' file.json | pretty print |
jq '.data' file.json | select field |
| `jq '.items[] | .id' file.json` |
jq 'length' file.json | count length |
jq -r '.name' file.json | raw string |
jq Filters (Useful)
| Filter | ใช้ทำอะไร |
|---|---|
| `.[] | select(.active==true)` |
map(.id) | transform array |
sort_by(.createdAt) | sort |
group_by(.type) | group |
{id, name} | project fields |
curl + jq Together
curl -sS https://api.example.com/users | jq '.[] | {id, email}'
curl -sS https://api.example.com/orders | jq '[.[] | select(.status=="paid")] | length'
Debugging HTTP
| เป้าหมาย | คำสั่ง |
|---|---|
| ดู request/response headers | curl -v URL |
| trace ละเอียด | curl --trace-ascii /tmp/trace.log URL |
| วัดเวลา | curl -w "dns:%{time_namelookup} connect:%{time_connect} total:%{time_total}\n" -o /dev/null -s URL |