DEV Community

Cover image for Mastering curl: The Swiss Army Knife of the Command Line
Khaled Md Saifullah
Khaled Md Saifullah

Posted on

Mastering curl: The Swiss Army Knife of the Command Line

If you have ever tested endpoints worked with APIs or uploaded files it is likely that someone advised you to "just use curl." However, what is curl exactly and why is it a tool that developers have on hand? Let's dissect it.

What is curl?

"curl" is an acronym for "client URL." To move data to and from a server use this command-line utility tool. It's like the browser on your terminal, but with a lot more control.

What we can do with curl?

  • Fetch data from APIs
  • Send POST requests
  • Upload files and images
  • Test authentication and headers
  • Work with different protocols (HTTP, HTTPS, FTP etc.)

In short: if you want to talk to a server, curl is your friend.

Why Developers Love curl?

  • Cross-platform: Works on Linux, macOS and Windows
  • Protocol-rich: Supports HTTP, HTTPS, FTP, SFTP, SCP and more
  • Powerful for testing: Great for debugging APIs without writing code
  • Scriptable: Perfect for automation and shell scripts

Basic Usage of curl

  1. Make a GET request
curl http://localhost:8000
Enter fullscreen mode Exit fullscreen mode

This fetches data from server.

  1. Make a POST request
curl -X POST http://localhost:8000/post \
  -d "name=John&age=25"
Enter fullscreen mode Exit fullscreen mode

-X POST: sets the request method to POST
-d: sends form data

  1. Send JSON data
curl -X POST http://localhost:8000/post \
  -H "Content-Type: application/json" \
  -d '{"username":"john doe","password":"secret"}'
Enter fullscreen mode Exit fullscreen mode
  1. Add authentication
curl -u username:password http://localhost:8000/protected
Enter fullscreen mode Exit fullscreen mode

or with a token

curl -H "Authorization: Bearer YOUR_TOKEN" http://localhost:8000/api
Enter fullscreen mode Exit fullscreen mode

Uploading a File with curl

Upload a single photo

curl -X POST http://localhost:8000/upload \
  -F "file=@myphoto.jpg"
Enter fullscreen mode Exit fullscreen mode

Upload with extra data

curl -X POST http://localhost:8000/upload \
  -F "file=@myphoto.jpg" \
  -F "userId=12345" \
  -F "description=Profile picture"
Enter fullscreen mode Exit fullscreen mode

Upload with authentication

curl -X POST http://localhost:8000/upload \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -F "file=@myphoto.jpg"
Enter fullscreen mode Exit fullscreen mode

Downloading Files with curl

Want to grab a file from the internet?

curl -O http://localhost:8000/file.zip
Enter fullscreen mode Exit fullscreen mode

-o: saves the file with the same name as on the server.

Or specify a custom name

curl -o myfile.zip http://localhost:8000/file.zip
Enter fullscreen mode Exit fullscreen mode

Debugging with curl

Sometimes you want to see what’s really happening under the hood

curl -v http://localhost:8000
curl -i http://localhost:8000
Enter fullscreen mode Exit fullscreen mode

-v: verbose mode (shows request/response details)
-i: includes response headers in the output

Conclusion

Curl is a developer's Swiss Army knife not just a tool. It streamlines operations and saves time for anything from file uploads and downloads to rapid API testing.

Use curl the next time you are automating, debugging or building. You will question how you managed without it once you are at ease with it.

Top comments (2)

Collapse
 
ghotet profile image
Jay

I'm pretty new to Linux and I didn't know I could upload files with curl. Handy! Thank you!

Collapse
 
kmsaifullah profile image
Khaled Md Saifullah

great to hear that