CURL is a command-line tool that is used to transfer data from or to a server using different protocols like HTTP, FTP, SMTP, and more. It is a popular tool in Unix and Linux environments that allows users to send and receive data over the internet. In this article, we will explore 10 examples of cURL command in UNIX and Linux with code examples.
Fetching a Web Page
One of the most common uses of cURL is to fetch web pages from the internet. The following command will fetch the web page and print it to the console.
curl https://www.example.com
Download a File from a Website
One of the most common uses of cURL is to download files from a website. The following example demonstrates how to download a file from a website using cURL.
curl -O https://example.com/file.zip
In this example, the -O option tells cURL to use the same filename as the remote file when saving the file to the local system.
Upload a File to a Website
CURL can also be used to upload files to a website. The following example demonstrates how to upload a file to a website using CURL.
curl -F 'file=@/path/to/local/file' https://example.com/upload
curl -X POST -F "file=@/path/to/file.txt" https://example.com/upload.php
In this example, the -F option tells cURL to send a file as a form field. The @ symbol specifies the path to the local file.
Get HTTP Header Information
CURL can be used to retrieve HTTP header information from a website. The following example demonstrates how to get HTTP header information using CURL.
curl -I https://example.com/file.zip
In this example, the -I option tells retrieve only the HTTP headers of 'file.zip' from the server.
Perform a POST Request
CURL can also be used to perform a POST request. The following example demonstrates how to perform a POST request using CURL.
curl -X POST -d 'name=john&age=25' https://example.com/api
In this example, the -X option specifies the HTTP method to use, and the -d option specifies the data to send in the request.
Perform a PUT Request
CURL can also be used to perform a PUT request. The following example demonstrates how to perform a PUT request using cURL.
curl -X PUT -d 'name=john&age=25' https://example.com/api/1
Authenticating with Basic Auth
If a website requires authentication using Basic Auth, you can use cURL to authenticate and access the content. The following command will authenticate using Basic Auth and fetch the web page.
curl -u username:password https://www.example.com/authenticated-page
Setting Headers
CURL allows you to set custom headers when making requests. The following command will set a custom header and fetch the web page.
curl -H 'Authorization: Bearer token' https://www.example.com
Following Redirects
If a website redirects to another page, cURL can follow the redirect and fetch the content. The following command will follow the redirect and fetch the web page.
curl -L https://www.example.com/redirect
Setting User Agent
CURL allows you to set a custom user agent when making requests. The following command will set a custom user agent and fetch the web page.
curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' https://www.example.com
Save the Downloaded File with a Different Name
If you want to save the downloaded file with a different name, you can use the following command:
curl -o newname.zip https://example.com/file.zip
This command will download the file from the given URL and save it as 'newname.zip' in the current directory.
Download Multiple Files
To download multiple files using cURL, you can create a text file containing the URLs of the files you want to download and use the following command:
Here, the -O option is used to save the downloaded files with their original names, -L is used to follow redirects, and -K is used to read the list of URLs from the file 'urls.txt.'
Resume a Download
If a download is interrupted or stopped, you can resume it using the following command:
curl -C - -O https://example.com/file.zip
This command will resume the download of 'file.zip' from where it was interrupted or stopped.
Limit Download Speed
If you want to limit the download speed of a file, you can use the following command:
curl --limit-rate 100k -O https://example.com/file.zip
This command will limit the download speed of 'file.zip' to 100 kilobytes per second.
Send Data as JSON
If you want to send data as JSON using cURL, you can use the following command:
curl -X POST -H "Content-Type: application/json" -d '{"name": "John", "age": 30}' https://example.com/api
This command will send the data as JSON to the server using the HTTP POST method.
cURL is a powerful tool that can be used for various purposes, such as downloading files, uploading data, sending data as JSON, authenticating with servers, and more. In this blog article, we have explored ten examples of cURL commands that are frequently used in Unix and Linux systems.
We learned how to download a file, save the downloaded file with a different name, download multiple files, resume a download, limit download speed, upload a file, send data as JSON, authenticate with basic authentication, retrieve HTTP headers, and follow redirects.
These commands can help you accomplish various tasks efficiently, and understanding their functionality can save you time and effort. The flexibility and versatility of cURL make it an essential tool for developers, system administrators, and anyone who needs to transfer data over the internet.
Labels: best practices, curl, linux tutorial
0 Comments:
Post a Comment
Note: only a member of this blog may post a comment.
<< Home