กลับไปหน้าสูตร
#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.comfollow redirect
curl -sS https://example.comsilent + show errors
curl -o out.json URLsave 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.jsonpretty print
jq '.data' file.jsonselect field
`jq '.items[].id' file.json`
jq 'length' file.jsoncount length
jq -r '.name' file.jsonraw 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 headerscurl -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