Operations 4 min read

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.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Mastering curl: Essential Commands for API Testing and Automation

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/json

multipart/form-data (file upload):

curl -F [email protected] -F name=allenjol http://www.ayunw.cn:2000/api/multipart

Submitting 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/api

Simple 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/login
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

HTTPcURLAPI testingcommand-line
Ops Development Stories
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.