Everything You Need to Know About cURL
Everything You Need to Know About cURL
CURL, which stands for Client URL, is a command line tool used by developers to transfer information to or from servers. According to IBM Developer Johanna Saladas, it “lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send.”
CURL supports various protocols including:
It runs on devices with a command line and network connectivity from a local server to edge. You can also use it on multiple operating systems such as Windows, Mac, and Linux.
This command is the libcurl development library and contains bindings for multiple codebases. CURL is also the title of the software project, which makes up the cURL command-line tool and libcurl development library.
Why Use CURL?
There are several benefits to using cURL. The software project is compatible with most available operating systems and works with numerous devices.
CURL is useful when testing points. It enables you to make sure they are working correctly. CURL provides details of the data that was sent or received, which is helpful if you need to debug.
Use cURL to send API requests. The endpoint is the URL that will receive the request. You can also use an HTTP method such as get, post, put, and delete.
Use “get” when retrieving a resource from the server. Sources include information, files, or images.
Use “post” to send data to the server. Again, this could be information, files, or images.
Use “put” to update or create a resource. Examples include updating a database record or information in a file.
Use “delete” to remove a resource. This can be an entire file or a single database entry.
These HTTP methods may result in other actions, depending on how the API is configured.
How Do You Install CURL?
Installing cURL will be different for each operating system. Linux distributions often install it by default. You can verify this by typing cURL in the terminal window, then press ENTER. If it isn’t there, you will receive a “command not found” error.
If you need to install it, here’s how:
- If you have an Ubuntu/Debian based system, type the commands “sudo apt update” and “sudo apt install curl.” If your system is CentOS/RHEL, the command is “sudo yum install curl.”
- On a Fedora system, type the command “sudo dnf install curl.”
Install CURL on a Mac
CURL comes pre-installed on MacOS. When Apple releases updates for this operating system, users also receive cURL updates. Install the cURL Homebrew package to ensure you have the most recent version. Once you’ve done that, type the command “brew install curl” to complete the process.
Install CURL on Windows
CURL is now installed by default on Windows 10 version 1803 and above. Access it directly from the command prompt. CURL project Windows binaries are also available for older versions of Windows. These are in a ZIP file, which you can download and extract.
Locate the folder called curl-<version number>-mingw, then move it to another location on your computer. For example, if your version number is 7.62.0-win 64, the folder would be called curl-7.62.0-win64-mingw. You can place it in the root directory or in another folder of your choice.
Next, add the cURL bin directory to the Windows Path environment variable. This ensures Windows will be able to find it when you type the command “curl” in the command prompt. Use these steps:
- Press WINDOWS KEY+R to open the Windows Run dialog.
- Type systempropertiesadvanced. This opens the Advanced System Properties.
- Click the “Environment Variables” button.
- Double-click Path from the System Variables section, then add the path C:\curl-<version number>-mingw\bin. Using the example above, the path would be C:\curl-7.62.0-win64-mingw\bin.
- If you are running Windows 10, click the “New” button to add this path. If you are running an older version of Windows, type a semicolon followed by the path. Using the previous example, it would look like this:
After you complete these steps, type curl to test the command. If all is working correctly, you will see this output:
If you want help or are looking for more information about curl, type curl –help or curl –manual.
How Do You Use CURL?
The basic syntax for cURL is “curl” followed by a URL. This fetches content found there and prints it on the terminal.
Fetching this information is cURL’s most basic operation. Here are a few other important things you can do with cURL:
In addition to printing the URL content to the terminal, you can choose to save it to a file. Use -o to do this. For example:
CURL saves the content and displays a progress bar that includes download statistics. These include the download speed and the time it took to download the information.
If you don’t want to provide a file name manually, you can let cURL figure it out for you. Do this using the -o option. Using the example above, type:
When using -o or -O, and a file with the same name already exists on your computer, cURL will overwrite the existing one with the new one.
If you have a file that is only partially downloaded, resume the download using the -c - option. Using the above example, you would type:
As is the case with most command line tools, cURL gives you the ability to combine various options. For instance, in the example above, you can combine -O and -C -. You would write it as -OC – so it would look like this.
curl -OC - http://ftp.belnet.be/mirror/videolan/vlc/3.0.4/macosx/vlc-3.0.4.dmg
If cURL receives a redirect when you make a request, a new request isn’t automatically sent to the new URL. Use http://www.facebook.com as an example.
When a request is made to this URL using cURL, the server will send a HTTP 3XX redirectto https://www.facebook.com. You will see the redirect URL, but the response body will be empty, resulting in an empty output.
Use the -L option to tell cURL to follow the redirect. Here is what your request should look like:
curl -L http://www.facebook.com/
The HTML content of the page will be shown.
Remember, cURL can only follow redirects if there is an HTTP redirect reply from the server. Here, the server must use a 3XX status code. This code uses the location header to signify the new URL.
The progress bar cURL displays when saving the output of a URL to a file may not be useful in every situation. One example of this is when you choose to hide the output using the command -vo /dev/null.
Use the -s header to hide the progress bar and display only the outputs you want to see. For Windows, it will look something like this:
curl -svo NUL https://www.myexample.com/
Be careful when using the -s option because it can hide error messages. If you want to hide the progress bar but see the error messages, try combining the -s option. Using the last example, you can save the output of https://www.myexample.com/ to an HTML file and hide the progress bar. It will look like this:
curl -sSvo file.html https://www.myexample.com/
Make POST Requests
CURL sends GET requests by default, but it will also send POST requests with the -d or -data option. All fields must be written as key=value pairs separated by the ampersand (&). Test this with a post request to httpbin.org using specified parameters. These look like this:
curl --data "firstname=my&lastname=example" https://httpbin.org/post
Here, two parameters were posted, a first name of my and last name of example followed by a URL for httpbin. Special characters such as %, @, or spaces that are included in the value should be manually URL-encoded.
To submit an email parameter containing the value firstname.lastname@example.org, type the following command:
curl --data "email=test%40example.com" https://httpbin.org/post
You can also use --data-urlencode to do this.
To use two parameters like email and name, type the following:
curl --data-urlencode "email@example.com" --data-urlencode "name=my example" https://httpbin.org/post
Data parameters that are too large can be saved to a file. In this case, use the @, command. This is what it would look like:
curl --data @params.txt example.com
Change the Request Method
You can send a post request without data. To do this, change the POST request using the -X option. It looks like this:
curl -X POST https://httpbin.org/post
The request method can be changed to anything else you choose such as patch, delete, or put. One exception to this rule is the head method, which you cannot set using the -X option.
The head method checks if a document is on the server without downloading it. Use the -I option for the head method. It looks like this:
curl -I https://www.myexample.com/
Here, all request headers are displayed by default. The server will not send any content when this request is made, so you will not see anything after the headers.
How Can You Find All the Options in CURL?
There are endless possibilities to what you can do with cURL. Type man curl in your terminal for a list of all the available options.