HTTPie is a command-line tool that makes it easy to interact with APIs from the terminal. This guide will show you how to use HTTPie to get information about a particular API, create an account, and make a request. First, install HTTPie on your computer. You can download it from the website or use the following command to install it on Ubuntu: sudo apt-get install httpie Once HTTPie is installed, you can open it by typing http in the terminal window. To get started with HTTPie, we first need to find an API we want to interact with. We can do this by using the –search option and specifying the name of an API. For example, if we wanted to find the GitHub API, we would type: http –search github This will return a list of all of the APIs that are available on GitHub. We can select one of these APIs by using the –select option and providing its name as an argument. For example, if we wanted to access the GitHub API again but this time use its credentials instead of just its name, we would type: http –select github –credentials “username:password” ..


HTTPie is an HTTP client for your terminal. Designed as a modern alternative to curl, HTTPie simplifies interaction with APIs by offering a straightforward syntax and automatically formatted output.

Available on Windows, Mac, and Linux, getting setup should be a straightforward affair. Most Linux distributions now include HTTPie within their package repositories, so you can quickly apt, dnf or pacman to begin. It’s also available via Homebrew for Mac users, and as a Python package delivered via Pip on all three platforms (pip install httpie).

Once installed, HTTPie registers the http and https commands in your shell. You’ve now got a simple and expressive way to call API endpoints without leaving your terminal!

Basic Commands

At its simplest, HTTPie can be passed a URL to immediately make a GET request:

To send data, specify the appropriate HTTP verb and then pass your key/value pairs as additional command-line parameters:

By default, data is sent as JSON with appropriate request headers. To submit as a form instead, pass the -f parameter.

When using the JSON syntax, be aware that all fields are normally sent as strings. You can use the := syntax instead of = to switch to raw data mode. A parameter examples:=’[1, 2]’ will then result in the examples key being set to an array of two integers.

Headers and Cookies

To set a request header, specify the header’s name and value as a colon-separated string:

HTTPie sets some headers, such as User-Agent, by default. These can be removed by explicitly specifying them with an empty value.

Cookies are set by defining a string with the cookies as colon-delimited values:

This is really just a special case of setting the Cookie header, which is how cookies are sent over HTTP.

Working With Files

You can upload and download files using standard shell redirects:

You can also upload files as part of an HTTP form submission by using the special @ syntax:

This will act identically to an HTML file input with name=“myUpload”. You may instead load data from a file and embed it into the request using the =@ syntax, instead of @.

Sessions

HTTPie has built-in support for persistent sessions. These allow you to reuse request components, such as HTTP headers and cookies, between requests made to the same host.

You create and use sessions by setting the –session parameter. As its value, specify the path to a file which will be used to store your new session.

Data which is supported by sessions, such as the Authorization header in the above request, will now be saved into the file. On subsequent requests, you may now omit the Authorization header – it’ll be included automatically as it’s defined in your session.

Instead of specifying a session file, you may also use a simple name (–session=example). In this case, HTTPie will automatically save the session to an internally managed file. Each session is tied to the host it originates from, so http –session=example example1.com and http –session=example example2.com will exist independently of each other.

Managing Output

One of HTTPie’s significant improvements over utilities like curl is its automatic formatting of responses. JSON bodies are particularly well-handled, with proper indentation, alphabetical sorting of objects by their keys, and correct conversion of Unicode characters.

You can customize how output is rendered using a few different options. The –pretty flag can be set to –all (default), –colors (only colors), –format (only formatting) or –none (to disable all output processing and see the raw data).

In addition, you can change the color scheme using the –style flag. The available schemes are auto (the default), default (use the underlying Pygments library styles), fruity and the popular monokai.

You don’t need to worry about the default formatting when redirecting output into a file. HTTPie will recognize that it’s being redirected and simply pipe the raw data without applying any formatting. This also means that binary responses, which are never normally emitted to the terminal, can be piped into files.

Configuration File

HTTPie supports a basic configuration file that can be used to define default settings. These will be applied to all requests you make. The file should be saved to ~/.config/httpie/config.json on Linux/Mac and %APPDATA%httpieconfig.json on Windows.

A single configuration key is supported, default_options, which accepts a basic array of parameters to append to HTTPie commands you execute:

Any option which is supported by HTTPie’s command-line interface can be included. You can override your default options by defining them with a new value each time you run HTTPie.

Conclusion

HTTPie is a feature-filled tool that brings HTTP APIs to your terminal. It’s a modern alternative to Unix staples like curl that’s designed for regular use by developers and testers. Although the syntax can be cumbersome at times, it’s generally expressive and memorable.

It’s worth taking the time to read the official documentation if you’re looking to learn more about HTTPie. All development occurs in the open on GitHub, with support provided on Gitter and StackOverflow.