• March 27, 2024

What Is Curl Command In Linux

curl command in Linux with Examples - GeeksforGeeks

curl command in Linux with Examples – GeeksforGeeks

curl is a command line tool to transfer data to or from a server, using any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP or FILE). curl is powered by Libcurl. This tool is preferred for automation, since it is designed to work without user interaction. curl can transfer multiple file at [options] [URL… ]URL: The most basic uses of curl is typing the command followed by the This should display the content of the URL on the terminal. The URL syntax is protocol dependent and multiple URLs can be written as sets like:curl one, two, three}
URLs with numeric sequence series can be written as:curl 1-20]
Progress Meter: curl displays a progress meter during use to indicate the transfer rate, amount of data transferred, time left -# -o curl –silent If you like a progress bar instead of meter, you can use the -# option as in the example above, or –silent if you want to disable it completely. Example:Options:-o: saves the downloaded file on the local machine with the name provided in the -o [file_name] [URL… ]
Example:curl -o Output:The above example downloads the file from FTP server and saves it with the name: This option downloads the file and saves it with the same name as in the -O [URL… ]
Example:curl -O –: This option resumes download which has been stopped due to some reason. This is useful when downloading large files and was -C – [URL… ]
Example:curl -C – -O limit-rate: This option limits the upper bound of the rate of data transfer and keeps it around the given value in –limit-rate [value] [URL]
Example:curl –limit-rate 1000K -O Output:The command limits the download to 1000K bytes. -u: curl also provides options to download files from user authenticated FTP -u {username}:{password} [FTP_URL]
Example:curl -u demo:password -O: This option helps to upload a file to the FTP -u {username}:{password} -T {filename} {FTP_Location}
If you want to append a already existing FTP file you can use the -a or –append option. –libcurl:This option is very useful from a developers perspective. If this option is appended to any cURL command, it outputs the C source code that uses libcurl for the specified option. It is the code similar to the command line [URL… ] –libcurl [filename]
Example:curl > –libcurl code. c
Output:The above example downloads the HTML and saves it into and the code in code. c file. The next command shows the first 30 lines of the code. -x, –proxy: curl also lets us use a proxy to access the -x [proxy_name]:[port] [URL… ]
If the proxy requires authentication, it can be used with the command:curl -u [user]:[password] -x [proxy_name]:[port] [URL… ]
Sending mail: As curl can transfer data over different protocols, including SMTP, we can use curl to send –url [SMTP URL] –mail-from [sender_mail] –mail-rcpt [receiver_mail] -n –ssl-reqd -u {email}:{password} -T [Mail text file]DICT protocol: The Libcurl defines the DICT protocol which can be used to easily get the definition or meaning of any word directly from the command [protocol:[dictionary_URL]:[word]
Example:curl dict
Output:Note: There are a number of other options provided by cURL which can be checked on the man page. The Libcurl library has been ported into various programming languages. It’s advisable to visit the individual project site for documentation.
Linux Curl Command - javatpoint

Linux Curl Command – javatpoint

Linux curl command is used to download or upload data to a server via supported protocols such as HTTP, FTP, IMAP, SFTP, TFTP, IMAP, POP3, SCP, etc. It is a remote utility, so it works without user interaction.
The data transfer from one place to another is one of the vital and most used tasks of a computer system. However, there are many GUI tools available for data transfer. But, when working on the command-line, it becomes a bit complicated. The curl utility allows us to transfer data via the command line.
Syntax:
The basic syntax for using curl is as follows:
From the above syntax,
URL:
The URL syntax is a general protocol-dependent URL. We can specify multiple URLs as follows:
Options:
The curl command supports the following command-line options:
–abstract-unix-socket : It is used to connect via abstract Unix domain socket instead of a network.
–anyauth: It is used to command curl for authenticating the method by itself for using the most secure method. This utility is an optional utility “–basic, –digest, –ntlm, and negotiate, ” which is used to set a specific authentication method.
-a, –append: It is used to upload files. It appends the file rather than overwriting it. If the given file does not exist on the server, it will create one.
–basic: It specifies the use of HTTP basic authentication. It is the default option of the curl command. It is useful for overriding the previous settings.
–cacert : It is specified for using the particular certificate file to verify the peer. The files may have several CA certificates. The standard format for the certificates is PEM, so, all the certificates must be in it.
–capath

: It is specified for using the particular directory to verify the peer. We can specify multiple paths by separating them by a colon (:) such as “path: path2:path3”. The standard format for the certificates is PEM, so, all the certificates must be in it.
–cert-status: It is used to verify the status of the server certificate. It uses Certificate Status Request or OCSP stapling TLS.
–cert-type : It specifies the provided curl certificate type. These certificates can be in PEM, DER, and ENG format. The default value is PEM. If it is specified multiple times, the last value will be taken by the curl.
-E, –cert : It is specified for using the client certificate file when getting a file via any of SSL-based protocol such as HTTPS, FTPS, etc.
–ciphers : It is used to select the ciphers to use in the connection.
–compressed-ssh: It is used to enable built-in SSH compression. This option will take as a request by the server, so the server may or may not accept it.
–compressed: It is used to request a compressed response using the curl algorithms and saves the uncompressed document. This option will send a report for unsupported encoding curl.
-K, –config : It is used to describe a text file for reading curl arguments. The curl will use the command line arguments from the text file.
–connect-timeout : It is used to specify the maximum time in seconds for curl connection time-out.
–connect-to : It is used to create a request to the given pair of host and port; otherwise, it will connect to the next pair. This option is a handy tool for making direct requests at a specific server.
-C, –continue-at : It is used to continue or resume a previous file transfer at the given offset.
-c, –cookie-jar : It is used to specify a particular file to which we want to write all cookies after a successful operation.
-b, –cookie : It is used to forward data to the HTTP server in the Cookie header.
–create-dirs: It is used for conjunction with the ‘-o? option, It will create the required local directory hierarchy.
–crlf (FTP SMTP): It is used to convert LF to CRLF in the upload. It is a handy tool for MVS (OS/390).
–crlfile : It is used to specify (in PEM format) with a Certificate Revocation List.
–data-ascii : It is an alias for the? -d? option.
–delegation : It is used to set LEVEL to acknowledge server what it is allowed to delegate when it comes to user credentials.
–digest: It is used to enable HTTP Digest authentication.
-q, –disable: If used as the first argument, It will ignore the curlrc config file.
–dns-interface : It is used to acknowledge the server to send the outgoing DNS requests.
–dns-servers : It is used to specify the DNS servers instead of default servers.
-f, –fail: It is used to make the curl silently fail on server errors.
-F, –form : It is used emulate a filled-in form submitted by user.
-P, –ftp-port

: It is used to reverse the default listener roles when connecting with FTP.
–ftp-ssl-ccc-mode : It is used to set the CCC mode.
-G, –get: It is used to specify data with? -d? option to be used in an HTTP GET request instead of POST request.
-h, –help: It is used to show the help manual having a brief description of the usage and support options.
-0, –1. 0: It is specified for using the HTTP version 1. 0.
–ignore-content-length: It is used to ignore the Content-Length header.
-i, –include: It is used to include the HTTP responses headers.
-4, –ipv4: It is used to resolve names to Ipv4 addresses.
-6, –ipv6: It is used to resolve names to Ipv6 addresses.
Installation of the curl Command
The curl command comes with most of the Linux distributions. But, if the system does not carry the curl by default. You need to install it manually. To install the curl, execute the following commands:
Update the system by executing the following commands:
Now, install the curl utility by executing the below command:
Verify the installation by executing the below command:
The above command will display the installed version of the curl command.
Fetch the content of the specified URL
To fetch the content of any specific URL, execute the curl command, followed by the URL. Consider the below command:
The above command will fetch the page data of the specified page. Consider the below snap of the output:
From the above output, we can see the page data of the given URL are being fetched. To stop the execution, press the CTRL+C keys.
Save data in a Specific File
To save the data in a specific file, pass the ‘-o’ option followed by directory, file name, and URL as follows:
Consider the below command:
The above command will save the page data in ” file under ” /home/javatpoint/Documents/” directory. Consider the below output:
From the above command, we can see the total amount of downloaded data, received data, average time, and some other statistics about the data.
To verify the downloaded data, open the file by executing the cat command
Consider the below snap of output:
Download a File From the Web
One of the interesting and fascinating uses of curl is that we can download a file from the web. To download a file from the web, copy the download link and paste it with the curl command. We can pass other arguments as well to make it more specific. For example, download the latest version of ubuntu, copy the download link of the ubuntu from its official website and paste it with curl command as follows:
The above command will download the Ubuntu 20. 04 to the specified directory. Provide the proper file extension; otherwise, it will download the file in a different format. Consider the below output:
From the above output, the file is being downloaded. We can see the download time, file size, download speed, and other statistics. To stop the execution, any time press the CTRL+D keys.
Resume the Interrupted Downloads
There may be a chance that the downloads can be interrupted for some reason. We can resume downloads by using the curl command. To resume the interrupted file, pass the ‘-C’ option with curl command as follows:
The above command will resume the download of the specified URL.
Download Multiple Files
To download the multiple files, specify the multiple URLs separated a by space as follows:
The above command will download the data from both URLs, respectively.
Query HTTP Headers
The HTTP headers contain additional information; it allows the webserver to download this information. To query the HTTP headers from a website, execute the command with ‘-I’ option as follows:
The above command will produce the below output:
What is the curl command? Learning and testing APIs with ...

What is the curl command? Learning and testing APIs with …

cURL, which stands for client URL, is a command line tool that developers use to transfer data to and from a server. At the most fundamental, cURL 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 several different protocols, including HTTP and HTTPS, and runs on almost every platform. This makes cURL ideal for testing communication from almost any device (as long as it has a command line and network connectivity) from a local server to most edge devices.
The most basic command in curl is curl. The curl command is followed by the URL, from which we would like to retrieve some kind of data. In this case, it would return the html source for
Underlying the curl command is the libcurl development library, which has bindings for almost any codebase.
cURL is also the name of the software project, which encompasses both the curl command-line tool and the libcurl development library.
Prerequisites
To try out the commands in this article, you need a command shell and internet access. We are going to be using the NASA APOD (Astronomy Picture Of the Day) API to create some examples. This API is open source but you will need to sign up for a developer key, which takes just a minute to get signed up.
Why use curl?
So, why should you use cURL? Consider these benefits of this software project:
It is highly portable. It is compatible with almost every operating system and connected device.
It is useful for testing endpoints, to check if they are working.
It can be verbose, providing details of exactly what has been sent/received, which is helpful for debugging.
It has good error logging.
It can be rate limited.
Sending API requests
We can use curl to send API requests. Each request is generally made up of four main parts:
An endpoint, which is the address (URL) to which we are sending the request.
An HTTP method. The most common methods used are GET, POST, PUT and DELETE.
GET is used to retrieve a resource from a server. This could be a file, information, or an image.
POST is used to send information to the server.
PUT can be used to create or update a resource. This could be used to create or update a record in a database or update the contents of a file.
DELETE is used to delete a resource such as a database entry.
These actions for these methods are the recommended actions, but it’s up to the API specification and implementation to define what exactly happens.
Headers, which contain metadata about the request, such as content type, user agent, and so on.
Body, which is the message body and contains the data that we want to send, if any. Generally, the body is used with POST and PUT methods.
curl command options
There are over two hundred curl options. You can see some of them by typing curl -h in a terminal. The most commonly used command options include these:
-I returns only the HTTPS headers
curl –request GET ‘&date=2020-01-01′ -I
This command will return header fields such as Date, Content-Type etc
-v is the verbose option
curl –request GET ‘NASA_API_KEY&date=2020-01-01’ -v
This verbose command will show you everything that happens when you run the curl command, from connection to the headers and any data returned. Here we also get the description of the image that is being returned by the request, along with the image url.
-o stores the output in a file
curl –request GET ‘NASA_API_KEY&date=2020-01-01’ –output curloutput
Combining curl with other CLI commands
Combining curl with other cli commands can be really handy in situations where you want to use the output of a command as the input to a curl command or vice versa.
As an example, you could see if a webpage contains a certain piece of text using curl and grep.
Here is an example of using curl and Python to extract the image link from a request to the NASA API and display it in the Preview app (MacOS only):
curl –request GET “NASA_API_KEY&date=2020-01-01” -s | python3 -c “import sys, json; print(()[‘url’])” | xargs curl -o && open -a Preview
In this example we use curl to make a GET request on the Nasa API endpoint. This returns json data, which we use in a small Python script to extract the url of the image. We then use the curl command to get the image and open it using Preview on the mac.
You don’t have to use the command line curl to make API requests. You can use a number of different tools to interact with an API, such as HTTPie, Postman, and Rest Client in VS Code.
HTTPie
HTTPie is a command-line HTTP client that is touted as more friendly to users. It also includes a more expressive, color-coded UI. They have an online version, which is really neat.
Postman
Postman is a UI-based client for all things related to API development, and it’s arguably one of the most popular. Postman is very powerful.
You can generate and execute curl commands from within Postman. To generate curl commands, you can enter the request URL and parameters, and then click on the code option on the right-hand side:
A box is displayed with the option to select from a number of languages, including curl. Select curl to see the generated curl command.
Postman gives you a history of all the requests that you’ve built and even data-stamps them, which can be nice for collecting data points or references as you work with an API. Think of it as the client taking notes for you.
Rest Client in VS Code
Rest Client for VS Code is probably one of my favorite tools for executing curl commands. It’s lightweight and has good syntax highlighting. It’s a really useful add-on to do some quick curl requests from within VS Code.
You can simply type in your curl command and a ‘send request’ option will appear above.
After you click send request, another tab opens with the response.
Summary and Next Steps
In this article, we introduced you to the basic curl command and its most useful options. We also mentioned only a handful of the tools that are available to help you get started with cURL. Now you can begin using cURL to test your endpoints and troubleshoot your applications.
To find out more about APIs and API Management, check out the following articles and videos available on IBM Developer:
API Fundamentals
What is a REST API
Acknowledgements
This article was originally authored by Amara Graham and published in April of 2019.

Frequently Asked Questions about what is curl command in linux

What is curl command used for in Linux?

Linux curl command is used to download or upload data to a server via supported protocols such as HTTP, FTP, IMAP, SFTP, TFTP, IMAP, POP3, SCP, etc. It is a remote utility, so it works without user interaction. The data transfer from one place to another is one of the vital and most used tasks of a computer system.

What is curl command?

cURL, which stands for client URL, is a command line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send.Feb 23, 2021

What is curl and how do you use it?

cURL is a command-line tool that you can use to transfer data via network protocols. The name cURL stands for ‘Client URL’, and is also written as ‘curl’. This popular command uses URL syntax to transfer data to and from servers. Curl is powered by ‘libcurl’, a free and easy-to-use client-side URL transfer library.Dec 23, 2020

Leave a Reply

Your email address will not be published. Required fields are marked *