# Use curl to interacting with an API by Seth Kenlon Curl is commonly referred to as a non-interactive web browser for the Linux terminal. Its developers, however, describe it more accurately as a tool to transfer data to or from a server, with access to a huge variety of protocols, including HTTP, FTP, SFTP, SCP, IMAP and POP3, LDAP, SMB, SMTP, and many more. It's a useful tool to the average sys admin, whether you use it for a quick way to download a file you need from the Internet, or to script automated updates. Curl is also an important tool for testing remote APIs, so if a service you rely on or provide is unresponsive, you can use the ``curl`` command to test it. ## API The term [API](https://opensource.com/resources/what-api) is short for *Application Programming Interface*, meaning that an API is anything that takes specific actions or responds to queries for information provided by end users. It's a powerful means of abstraction, and can be the difference between giving someone, for example, access to your database server and just letting people request information from your database through a highly structured URL. An API also enables people to use features of a service without requiring them to learn complex commands nor exposing sensitive data. And more importantly, an API allows computers to talk to computers in a structured and programmed way, so that instead of you spending *your* time scraping the web for data, you can script it. ## Installing curl It's quite likely that you already have Curl installed. If you don't have it installed, it's probably in your software repository. On Fedora, CentOS, or RHEL: ``` $ sudo dnf install curl ``` ## Curl basics You can download a file with Curl by providing a link to a specific URL. Whatever exists at the URL you provide is, by default, downloaded and printed in your terminal. HTML is relatively verbose, so that's often a lot of text. You can pipe the output to ``less`` or ``tail`` or any other command you find useful. ``` $ curl "http://example.com" | tail -n 4

Example Domain

This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.

More information...

``` Some URLs contain special characters that your shell interprets as part of a command. To avoid unexpected behaviour, surround your URL in quotation marks. Sometimes, you don't want to read a file in your terminal. Use the ``--remote-name`` option to save a file according to what it's called on the server: ``` $ curl --remote-name "https://example.com/linux-distro.iso" [...] $ ls linux-distro.iso ``` Alternatively, use the ``--output`` option to rename your download before it's saved: ``` curl "http://example.com/index.html" \ --output example-index.html ``` ## Get HTML headers HTTP headers are components of the initial portion data sent over HTTP. It often contains important information, including the server response code (such as 200, 301, 404, and so on), but it's not visible through everyday web interactions. You can see it all with Curl: ``` curl --head --show-error "https://example.com" HTTP/2 200 accept-ranges: bytes age: 485487 cache-control: max-age=604800 content-type: text/html; charset=UTF-8 date: Sun, 26 Apr 2020 09:02:09 GMT etag: "3147526947" expires: Sun, 03 May 2020 09:02:09 GMT last-modified: Thu, 17 Oct 2019 07:18:26 GMT server: ECS (sjc/4E76) x-cache: HIT content-length: 1256 ``` If you need faster response times, you can use ``--fail-early`` to cause Curl to fail instead of spending cycles trying to resolve unresponsive servers. ## Query an API endpoint A simple query to an API endpoint is technically as simple as the most basic Curl command. You point Curl at the API gateway URL, and ideally get the default response of the API. Not all APIs provide a response, but here's a good example: ``` $ curl "https://gitlab.com/api/v4/projects" | less ``` This query fetches a JSON data dump of a list of recent projects on Gitlab.com, which you can pipe through ``less`` or `[jq](https://opensource.com/article/20/4/how-use-jq-tool-editing) or some other pager or parser. ## Send form data (emulates a form and Submit button) You can also send commands with Curl. For example, for an API behind a login screen, you can use the ``--form`` option to pass your credentials before accessing the data you need. This example isn't advisable, because your password would appear in your Bash history, although you can [configure your shell history to ignore commands preceeded by a space](https://www.redhat.com/sysadmin/parsing-bash-history) to safeguard against this (as long as you do indeed preceed the command with a blank space). ``` $ curl --form "username=seth" --form "password=12345678" "https://example.com/api/v4/endpoint" ``` ## Send contents of a file as form data Another way to pass athorization is with Curl's special ``<`` notation, which takes data from inside an existing file that you have access to: ``` $ curl --form "description=