Posted on
Advanced

Using API calls from Bash with curl or wget

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Mastering API Calls from Bash: Using curl and wget

In today's interconnected technological environment, APIs (Application Programming Interfaces) serve as the backbone of software communication. From fetching data for a weather app to automating system operations, knowing how to interact with APIs is an invaluable skill. For Linux users, the Bash shell provides powerful tools such as curl and wget for making API calls directly from the command line. In this blog, we'll explore how to use these tools across different Linux distributions.

Installing curl and wget

Before we delve into making API calls, ensure that curl and wget are installed on your system. Both tools are commonly pre-installed in many Linux distributions; however, if you find them missing, you can easily install them using one of the following package managers:

Debian/Ubuntu (using apt):

sudo apt update
sudo apt install curl wget

Fedora, CentOS, RHEL (using dnf):

sudo dnf install curl wget

openSUSE (using zypper):

sudo zypper install curl wget

Making API Calls with curl

curl is a versatile command-line tool used to transfer data to or from a server. It supports numerous protocols including HTTP, HTTPS, FTP, and more. Here’s how to use curl to make API calls.

Basic GET request

To retrieve data from an API, you can perform a GET request. Here's a simple example that fetches user information from a hypothetical API:

curl https://api.example.com/users/1234

Including headers

Some APIs require you to include headers (like authorization tokens). Use the -H flag to include headers in your curl command:

curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.example.com/users/1234

POST request with JSON data

For sending data (like creating a new record) you can use a POST request. Here’s an example where JSON data is being sent to the server:

curl -X POST -H "Content-Type: application/json" -d '{"name":"John", "age":30}' https://api.example.com/users

Making API Calls with wget

While curl is typically more flexible, wget is excellent for simple retrieval operations. It’s particularly good at downloading files but can also be used for API interactions.

Basic GET request

You can use wget to make a basic GET request as follows:

wget -qO- https://api.example.com/users/1234

-q turns off wget's output, while -O- tells wget to write the output to standard output.

Downloading files

wget is particularly useful for downloading files. Just pass the file URL to wget:

wget https://example.com/file.zip

Choosing between curl and wget

  • Use curl if you need a tool with extensive protocol support and fine-grained control over headers, request methods, user credentials, and other elements.

  • Choose wget if your primary task is robust file downloading. It can recursively download files and even mirror complete websites.

Conclusion

Both curl and wget are powerful tools for making API calls and handling various data transfer tasks across networks. Whether you are accessing simple data or performing complex interactions with REST APIs, mastering these tools will streamline your command line operations and expand the capabilities of your scripts.

As APIs continue to evolve as a crucial part of software and network operations, integrating Bash tools like curl and wget into your workflows ensures that you are equipped to handle data efficiently and effectively in a Linux environment.