DevOps Blog

A Beginner's Guide to Using `curl` in Linux

What is curl?

curl (short for "Client URL") is a command-line tool for transferring data using various network protocols. It’s pre-installed on most Linux distributions and is widely used for tasks like:

  • Downloading files
  • Sending HTTP requests (GET, POST, PUT, DELETE)
  • Testing REST APIs
  • Debugging network connections

Basic Syntax

The simplest way to use curl is:

curl [options] [URL]

1. Downloading a File

To download a file from a URL:

curl -O https://example.com/file.zip
  • -O saves the file with its original name.

If you want to specify a different filename:

curl -o custom_name.zip https://example.com/file.zip

2. Making HTTP Requests

curl can send different types of HTTP requests.

GET Request (Default)

curl https://api.example.com/data

POST Request (Sending Data)

curl -X POST -d "name=John&age=30" https://api.example.com/users
  • -X POST specifies the HTTP method.
  • -d sends data in the request body.

Sending JSON Data

curl -X POST -H "Content-Type: application/json" -d '{"name":"John","age":30}' https://api.example.com/users
  • -H adds a header (like Content-Type).

3. Following Redirects

Some URLs redirect to another location. Use -L to follow them:

curl -L https://example.com

4. Downloading with Authentication

If a site requires a username and password:

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

5. Saving Cookies & Sessions

To save cookies to a file:

curl -c cookies.txt https://example.com

To use saved cookies in a subsequent request:

curl -b cookies.txt https://example.com/dashboard

6. Testing Headers Only

If you only want to see the response headers (not the body):

curl -I https://example.com

7. Limiting Download Speed

To avoid overloading a server (or your bandwidth), limit download speed:

curl --limit-rate 100K -O https://example.com/largefile.zip
  • --limit-rate 100K restricts speed to 100 KB/s.

Conclusion

curl is an essential tool for any Linux user working with web requests, APIs, or file downloads. Once you master the basics, you can automate tasks, debug APIs, and interact with remote servers efficiently.

Want to learn more? Check man curl for advanced options!

**Happy curling! **


Would you like me to add any specific examples or use cases? Let me know!