Home Learn Linux Master cURL Commands: Essential and Advanced Options

Master cURL Commands: Essential and Advanced Options

In this guide, we'll explore both essential and advanced cURL commands that will help you streamline your workflow and get more done in less time. From basic commands to more advanced options, you'll find plenty of useful tips and tricks to take your skills to the next level.

by Arun Kumar
curl command

If you’re looking to become a more efficient and effective web developer, then mastering cURL commands is a must. cURL is a powerful command-line tool that allows you to transfer data to and from a server, and it’s an essential tool for anyone working with APIs, web services, or automating tasks.

This guide is designed to help you improve your skills and productivity by providing both essential and advanced cURL commands. Regardless of your experience level, you’ll find a wealth of useful tips and tricks to take your abilities to the next level. So buckle up and get ready to streamline your processes with cURL!

What is cURL?

Before we get our hands dirty with commands, let’s quickly talk about what cURL is. cURL stands for Client URL, and it’s used to transfer data to and from a server. It supports a plethora of protocols, including HTTP, HTTPS, FTP, and more. Whether you’re downloading files, querying APIs, or just poking around servers, cURL is your go-to tool.

Basic syntax of cURL

The basic syntax of a cURL command is pretty straightforward:

curl [options] [URL]

At its simplest, you can use cURL to retrieve the content of a webpage. Here’s an example:

$ curl http://example.com

This command will fetch the HTML content of example.com and display it in your terminal. Simple, right?

Essential options in cURL

Now, let’s spice things up with some essential options that I find incredibly useful:

-O (Remote file download)

One of my personal favorites is the -O option, which downloads the remote file to your local machine. It saves the file with the same name as the remote server.

$ curl -O http://example.com/somefile.txt

This command will download somefile.txt from example.com and save it in your current directory.

-o (Save file with a different name)

If you’re not a fan of the original file name or need to organize downloads into specific names, -o comes to the rescue.

$ curl -o localfile.txt http://example.com/somefile.txt

This saves the remote file as localfile.txt on your local machine. Neat for keeping things organized!

-L (Follow redirects)

Ah, the pesky redirects! Some URLs redirect you to another URL, and by default, cURL doesn’t follow them. But with -L, you’re covered:

$ curl -L http://example.com

This tells cURL to follow any redirects until it reaches the final destination.

-d (Sending POST data)

For interacting with APIs or web forms, -d is your friend. It allows you to send POST data to the server.

$ curl -d "param1=value1&param2=value2" http://example.com/form

This sends the provided data as POST to the specified URL. Very handy for testing web forms or APIs!

-H (Adding custom headers)

Sometimes, you need to include custom headers in your request, like Content-Type or authentication tokens. -H makes it a breeze:

$ curl -H "Content-Type: application/json" -H "Authorization: Bearer YourTokenHere" http://example.com/api

This sends a request with the specified headers, which is crucial for many API interactions.

-X (Specifying request command)

While cURL defaults to GET requests, you can use -X to specify a different method, such as POST, PUT, DELETE, etc.

$ curl -X POST http://example.com/resource

This explicitly sets the request method to POST.

Real-world example

Let’s put it all together with a real-world example. Say you want to download a file, but it’s behind a redirect and requires a custom header for access:

$ curl -L -H "Authorization: Bearer YourTokenHere" -O http://example.com/redirectedfile.zip

This command combines the following redirects, setting a custom header and downloading the file with the original name.

Advanced uses of cURL

Diving deeper into the cURL toolbox, let’s explore some advanced uses that can significantly enhance your command-line prowess. These examples showcase cURL‘s versatility and its ability to handle complex tasks with ease.

Working with APIs

API interaction is a common task for developers, and cURL shines in this area. Let’s say you want to consume a RESTful API:

$ curl -H "Content-Type: application/json" -H "Authorization: API_KEY" -X GET "https://api.example.com/data"

In this command, we’re making a GET request to an API endpoint, passing in a content type of JSON and an authorization header. This pattern is fundamental when working with APIs that require authentication.

Uploading files

cURL can also upload files to a server using multipart/form-data, which is typical in file upload forms:

$ curl -F "file=@localfile.zip" http://example.com/upload

This command uploads localfile.zip to the server. The @ symbol indicates a file on your local filesystem. This is incredibly useful for automating uploads or integrating with CI/CD pipelines.

Saving cookies

When dealing with websites that require authentication or maintain sessions, cookies become essential. cURL can save and use cookies like this:

# Save cookies
$ curl -c cookies.txt -X POST -d "username=user&password=pass" http://example.com/login

# Use cookies for subsequent requests
$ curl -b cookies.txt http://example.com/dashboard

First, we log in to a site and save the cookies to cookies.txt. Then, we use those cookies for the next request to access authenticated areas.

Rate limiting

When you’re hitting an API or a server, it’s courteous (and sometimes necessary) to rate-limit your requests. While cURL doesn’t have built-in rate limiting, you can achieve this with a combination of cURL and sleep in a bash loop:

for i in {1..10}; do
  curl -H "Authorization: API_KEY" "https://api.example.com/data?page=${i}"
  sleep 1  # Sleep for 1 second between requests
done

This loop makes ten requests to an API, pausing for one second between each request to avoid overwhelming the server.

Debugging with verbose mode

When things don’t go as planned, cURL‘s verbose mode can be a lifesaver. It provides detailed information about the request and response, helping you debug issues:

$ curl -v http://example.com

Verbose mode outputs the request headers, response headers, and many other details that can help identify problems.

Parallel requests with xargs

For executing multiple cURL requests in parallel, you can combine it with xargs. This is particularly useful for batch downloading or testing:

echo url1 url2 url3 | xargs -n 1 -P 3 curl -O

This command downloads three URLs in parallel (-P 3). Each URL is passed to cURL with -O to download the files.

Conclusion

Mastering cURL commands can be a game-changer for web developers, allowing you to automate tasks, work with APIs, and transfer data with ease. By understanding both essential and advanced cURL commands, you can become more efficient and effective in your work, saving time and reducing errors.

Whether you’re new to cURL or have some experience, this guide has provided you with a range of tips and tricks to help you take your skills to the next level. From basic commands to more advanced options, you now have the knowledge and tools you need to get more done in less time. So why wait? Start exploring cURL commands today and see how they can transform your web development workflow!

You may also like

1 comment

Jalal Hajigholamali February 26, 2024 - 4:00 AM

Hi,
Very useful article
Thanks a lot

Reply

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.