In this article, you will learn how to use HTTPie, a modern and user-friendly command-line HTTP client that is designed to be used with web APIs.
HTTPie is a command-line HTTP client built for modern web APIs. It provides intuitive commands and a user-friendly interface. In this guide, you learn about HTTPie's features and how it compares to cURL. You also learn how to install and start using HTTPie on your Linux system.
Before You Begin
- This article will use Linode Compute Instance. Create an account to follow along.
sudo. If you’re not familiar with the
sudocommand, see the Linux Users and Groups guide.
What is HTTPie?
HTTPie is a command-line HTTP client similar to cURL. But unlike cURL, HTTPie is designed to make working with modern web APIs easier. HTTPie's syntax is geared towards working with web services. Its interface is built to be more readable and user-friendly than cURL. These features make HTTPie a great tool for testing, debugging, or otherwise working with web services from the command line.
HTTPie vs curl
This section explores why you might choose to use HTTPie over cURL, especially since cURL is installed by default on many Linux distributions. cURL excels at covering a wide range of HTTP needs through a proliferation of options. HTTPie focuses on supporting queries against modern web APIs. It provides the most relevant details when working with web APIs and hides information that you are unlikely to need. HTTPie's focused output lets you interact with web services more intuitively and clearly.
If you are looking to work with web APIs, especially RESTful APIs that use JSON data you should consider using HTTPie. Alternatively, consider cURL if you want an HTTP client that fits more general needs, since it comes with options to make it more adaptable.
How to Install HTTPie
HTTPie is available from the package managers of most major Linux distributions. Below are the commands you can use to install HTTPie through the package managers of various distributions.
On Debian and Ubuntu, use the following command:
sudo apt install httpie
On AlmaLinux and CentOS, use the following command:
sudo yum install httpie
On Fedora, use the following command:
sudo dnf install httpie
Once you have HTTPie installed, you can verify your installation and access it using the
How to Use HTTPie
If you are familiar with other HTTP clients, HTTPie should be easier to learn. Much of its basic usage is similar to cURL`, but it allows you to simplify the required commands.
The sections that follow introduce you to working with HTTPie for the most common kinds of HTTP requests when working with web services.
You can see the most basic usage of HTTPie with GET requests. Like cURL, HTTPie does not require that you specify the request method. Simply provide the command followed by the request URL.
The following example request fetches a random fact about dogs from a "Dog Facts" web service. The request includes a URL parameter for the number of facts to be returned by the web service.
HTTPie's output includes response header information by default. It uses syntax highlighting to make the response even easier to read, as you can see in the following screenshot:
Adding header data to a request in HTTPie requires appending the data after the URL, as shown in the command below. The following example fetches a random "dad joke" from a web service. You can see that the command also adds the
--follow option, which has HTTPie follow any URL redirects (the equivalent of
curl). HTTPie includes the header information for the redirect response in its output.
http --follow icanhazdadjoke.com/ 'Accept: application/json'
To specify a request method in HTTPie, just include the method designation —
DELETE, etc — as the first part of the
http command. You can see this in the next example, which uses HTTPie's own web service for testing.
http DELETE pie.dev/delete
Submit JSON Data
Because HTTPie is designed with modern web APIs in mind, it makes submitting JSON data exceptionally easy. For the most basic requests provide the request's parameters after the URL. HTTPie automatically assembles the parameters into a JSON object.
The following example sends a POST request that capitalizes the letters in a string provided in JSON format. The response is a JSON object with the original string (
INPUT) and its capitalized version (
http POST api.shoutcloud.io/V1/SHOUT INPUT="hello, world!"
HTTPie also provides options for using files for the request and response JSON objects. You can see this feature in action by creating a JSON file like the one below for the "Shout Cloud" service used above.
Pie to submit that JSON data in a
POST request to the web service.
http POST api.shoutcloud.io/V1/SHOUT < to-shout.json
Conversely, you can have HTTPie save a JSON response as a file, as shown below:
http POST api.shoutcloud.io/V1/SHOUT INPUT="another shout over here!" > shouting.json
You should now be ready to start using HTTPie to send request to web APIs. If you find yourself looking for more advanced features, you can find them as you dig deeper into HTTPie. Check out the official HTTPie documentation, and also reference the
http --help command to start learning more about HTTPie's features.
If you found the article helpful, consider sharing it with those who might find it interesting.
Thanks for reading : )
Source: Linode Docs License: CC 4.0 Changes Applied: None