Lately, I’ve been falling in love with using curl
instead of Postman for testing REST APIs in development environment. curl
is a powerful tool for making HTTP requests from the command line.
curl
Usagecurl -X GET "https://api.example.com/data"
If the parameters are dynamic or if you prefer to keep the URL cleaner, you can also use the --data-urlencode
option:
curl -G "https://api.example.com/data" \
--data-urlencode "param1=value1" \
--data-urlencode "param2=value2"
curl -X POST https://api.example.com/data -d "param1=value1¶m2=value2"
You can include query parameters directly in the URL:
curl "https://api.example.com/data?param1=value1¶m2=value2"
Add headers using the -H
option:
curl -H "Authorization: Bearer token" -H "Content-Type: application/json" https://api.example.com/data
Send data using -d
as application/x-www-form-urlencoded
:
curl -X POST https://api.example.com/data -d "param1=value1¶m2=value2"
For JSON data, use:
curl -X POST https://api.example.com/data -H "Content-Type: application/json" -d '{"param1":"value1", "param2":"value2"}'
Use the -F
option to upload files:
curl -F "file=@/path/to/file" https://api.example.com/upload
Basic Authentication:
curl -u username:password https://api.example.com/data
Bearer Token:
curl -H "Authorization: Bearer token" https://api.example.com/data
curl -I https://api.example.com/data
curl https://api.example.com/data -o output.json
curl -i https://api.example.com/data
curl -H "Header1: value1" -H "Header2: value2" https://api.example.com/data
Useful for debugging:
curl -v https://api.example.com/data
curl -L https://api.example.com/redirect
Set a maximum time allowed for the transfer:
curl --max-time 30 https://api.example.com/data
curl
with Authenticationcurl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/data