Mastering curl: Essential Commands for API Testing and Automation
This guide explains how to use curl for sending GET, POST, and other HTTP requests, covering headers, data payloads, authentication, file uploads, and submitting data from files with clear command examples.
curl is a versatile command-line tool for sending HTTP requests, useful for testing APIs with GET, POST, and other methods.
Basic syntax: curl -H "Header" -d "Data" -X METHOD URL. Common parameters include -H (header), -d (data), and -X (request method such as POST, GET, DELETE, PUT, etc.).
Examples:
application/x-www-form-urlencoded :
curl -X POST -d 'name=allenjol' http://www.ayunw.cn:2000/api/user
curl http://www.ayunw.cn:2000/login -H "Content-Type:application/json" -X POST -d '{"user":"admin","passwd":"12345678"}'application/json :
curl -H "Content-Type: application/json" -X POST -d '{"id":"001","name":"allenjol","phone":"15188888888"}' http://ayunw.cn:2000/api/jsonmultipart/form-data (file upload):
curl -F [email protected] -F name=allenjol http://www.ayunw.cn:2000/api/multipartSubmitting data from a file : store JSON in data.json and use -d @data.json with the appropriate Content-Type header.
GET with basic authentication :
curl -X GET -u username:password http://example.com/apiSimple GET and POST examples :
curl http://127.0.0.1:8080/login?admin&passwd=12345678
curl -d "user=admin&passwd=12345678" http://127.0.0.1:8080/loginSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
