Skip to content
PotatoScript edited this page May 5, 2025 · 2 revisions

curl

curl (short for Client URL) is a command-line tool used to send and receive data to/from servers over various protocols, most commonly HTTP or HTTPS.

πŸ’‘ In Simple Terms:

curl lets you test or interact with web APIs or websites from the terminal.


πŸ”§ Common Use Cases:

Task Example Command
πŸ” Get a webpage or API data curl https://example.com
πŸ“€ Send JSON data to an API curl -X POST -H "Content-Type: application/json" -d '{"name":"Lucy"}' https://api.example.com
πŸ“„ Download a file curl -O https://example.com/file.pdf
πŸ“¬ Send form data curl -d "username=admin&password=1234" https://example.com/login

πŸ§ͺ Example (Real-Life Use):

To test your own local Japanese vocabulary app backend:

curl http://localhost:5000/api/vocab

It will return all vocab entries in your database (like a test client calling your API).


βœ… 1. Check if curl is installed

curl --version

If it's not installed:

  • Ubuntu: sudo apt install curl
  • Windows (WSL): Should be preinstalled
  • Windows CMD/Powershell: Also supported natively in recent versions

🌐 2. Make a GET Request

curl https://jsonplaceholder.typicode.com/posts

This fetches data from a fake REST API.

Tip: Use | less to scroll the result

curl https://jsonplaceholder.typicode.com/posts | less

🧾 3. Save Output to a File

curl https://example.com -o example.html

πŸ’Œ 4. Send a POST Request with JSON

curl -X POST https://jsonplaceholder.typicode.com/posts \
-H "Content-Type: application/json" \
-d '{"title":"Hello","body":"World","userId":1}'

πŸ—ƒ 5. Send Form Data (like login info)

curl -X POST -d "username=admin&password=1234" https://example.com/login

πŸ” 6. Set Headers (e.g., for authentication)

curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/data

πŸ”„ 7. Follow Redirects

curl -L https://bit.ly/3URL

πŸ§ͺ 8. Test Your Local API

curl http://localhost:5000/api/items

To test POST:

curl -X POST http://localhost:5000/api/items \
-H "Content-Type: application/json" \
-d '{"name":"Potato"}'

🧠 9. More Useful Options

  • -I – Only show HTTP headers: curl -I https://example.com
  • -u – Basic auth: curl -u username:password https://example.com
  • -v – Verbose/debug: curl -v https://example.com

🧾 Basic curl Cheat Sheet

πŸ“₯ GET request (default)

curl https://example.com

πŸ“€ POST request (send form data)

curl -X POST -d "username=lucy&password=1234" https://example.com/login

πŸ“€ POST JSON data

curl -X POST -H "Content-Type: application/json" \
-d '{"word": "ζŒ‘γ‚€", "meaning": "to challenge"}' \
https://api.example.com/vocab

🧾 GET with headers

curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/data

πŸ“Ž Upload a file

curl -F "file=@path/to/file.jpg" https://example.com/upload

πŸ’Ύ Save output to a file

curl -o output.html https://example.com

πŸ’‘ Follow redirects

curl -L https://example.com

πŸ›  Debug / show full request and response

curl -v https://example.com

🧠 See only headers

curl -I https://example.com

πŸ” Auth Examples

Basic Auth

curl -u username:password https://example.com

πŸ§ͺ Test Local Backend (Example)

curl http://localhost:8000/api/items