article thumbnail
Mastering HTTPie: The Modern CLI HTTP Client
A command-line HTTP client that makes CLI interaction with web services as user-friendly as possible
#commandlinetools, #httpie

HTTPie (pronounced "aitch-tee-tee-pee") is a command-line HTTP client that makes CLI interaction with web services as user-friendly as possible. It provides a simple interface for sending HTTP requests using intuitive syntax, with beautiful and colorful terminal output. If you've been using curl but find its syntax cumbersome, HTTPie might be the upgrade you've been looking for.

Why Choose HTTPie?

Before diving into the details, let's understand why HTTPie has gained popularity among developers, DevOps engineers, and system administrators:

  • Intuitive syntax that feels natural and readable
  • Beautiful output with syntax highlighting and formatting
  • JSON support built-in by default
  • Sensible defaults that reduce boilerplate
  • Extensive plugin ecosystem for specialized use cases
  • Cross-platform availability on all major operating systems

Installation

HTTPie is available on most platforms. Here's how to install it on various systems:

macOS

brew install httpie

Ubuntu/Debian

apt-get update
apt-get install httpie

Fedora

dnf install httpie

CentOS/RHEL

yum install epel-release
yum install httpie

Arch Linux

pacman -S httpie

Windows

Using Chocolatey:

choco install httpie

Using Scoop:

scoop install httpie

Python (Universal)

pip install --upgrade httpie

From Source

git clone https://github.com/httpie/httpie.git
cd httpie
pip install -e .

Basic Syntax

The basic syntax for HTTPie is:

http [flags] [METHOD] URL [ITEM [ITEM]]

Where:

  • [flags] are optional parameters that modify the request behavior
  • [METHOD] is the HTTP method (default: GET)
  • URL is the target URL
  • [ITEM] can be headers, query parameters, data fields, etc.

Simple Examples

# Basic GET request
http httpie.io/hello

# Explicit GET request
http GET httpie.io/get

# Other HTTP methods
http POST httpie.io/post
http PUT httpie.io/put
http DELETE httpie.io/delete
http PATCH httpie.io/patch

Request Data Types

HTTPie makes it easy to specify different types of request data.

Custom Headers

Add custom headers using the Header:Value syntax:

http example.com User-Agent:MyClient X-API-Token:abc123

URL Parameters

Add URL parameters with the == syntax:

http example.com/api search==python language==en

This sends a request to example.com/api?search=python&language=en

JSON Data (Default)

HTTPie sends JSON by default. Use the = syntax for string values:

http POST api.example.com/users name=John age:=30 is_member:=true

Note the difference:

  • name=John: String value ("John")
  • age:=30: Raw JSON number (30)
  • is_member:=true: Boolean value (true)

Form Data

Send form data with the -f or --form flag:

http --form POST api.example.com/users name="John Doe" email=john@example.com

File Uploads

Upload files using the @ syntax:

# Text file
http --form POST api.example.com/upload file@/path/to/file.txt

# Binary file with content type
http --form POST api.example.com/upload file@/path/to/image.jpg;type=image/jpeg

Authentication

HTTPie supports various authentication methods:

Basic Authentication

# Using credentials directly
http -a username:password example.com

# Using URL format
http username:password@example.com

Bearer Token

http example.com "Authorization: Bearer token123"

Digest Authentication

http --auth-type=digest -a username:password example.com

Advanced Options

Custom HTTP Methods

http --method=PURGE example.com

Following Redirects

http -F example.com
# or
http --follow example.com

Timeouts

http --timeout=30 example.com

Output Control

# Save to file
http example.com > response.json

# Download mode
http --download example.com/file.zip

# Headers only
http --headers example.com

# Body only
http --body example.com

# Verbose output
http -v example.com

# Extra verbose
http -vv example.com

Sessions

HTTPie can maintain sessions between requests:

# Login and save session
http --session=logged-in -a username:password example.com/login

# Use saved session
http --session=logged-in example.com/dashboard

Working with APIs

HTTPie shines when working with APIs. Here are some common patterns:

RESTful Operations

# Create a resource (POST)
http POST api.example.com/posts title="Hello World" body="This is my first post" userId:=1

# Retrieve a resource (GET)
http api.example.com/posts/1

# Update a resource (PUT/PATCH)
http PATCH api.example.com/posts/1 title="Updated Title"

# Delete a resource (DELETE)
http DELETE api.example.com/posts/1

GraphQL

http POST api.example.com/graphql query='{ user(id: 1) { name, email } }'

WebSocket Support

Using the HTTPie CLI extension httpie-websocket:

pip install httpie-websocket
http --ws ws://echo.websocket.org

Configuration

HTTPie can be customized through configuration files.

Configuration Locations

  • Config file: ~/.config/httpie/config.json
  • Sessions: ~/.config/httpie/sessions/<host>/<n>.json

Example Configuration

{
  "default_options": [
    "--style=monokai",
    "--follow",
    "--timeout=30",
    "--verify=no"
  ],
  "implicit_content_type": "json"
}

Command-Line Options Reference

Request Options

Option Description
-m, --method METHOD Specify HTTP method
--follow, -F Follow redirects
--verify VERIFY SSL verification mode
--ssl=SSL_VERSION Specify SSL version
--cert CERT SSL client certificate
--cert-key CERT_KEY SSL client certificate key
--ignore-netrc Ignore credentials from .netrc
--proxy PROXY Specify proxy
--http-version HTTP_VERSION HTTP version (1.0/1.1/2)
--cookies Enable cookies

Authentication Options

Option Description
-a, --auth USER:PASS Authentication credentials
--auth-type AUTH_TYPE Authentication mechanism
--bearer TOKEN Bearer token

Data Options

Option Description
-j, --json Data as JSON (default)
-f, --form Data as form
--multipart Multipart form data
-b, --body BODY Raw request body
--compress Compress request body
-c, --content-type CONTENT_TYPE Override content type

Output Options

Option Description
-p, --print WHAT What parts to print
-h, --headers Print only headers
-b, --body Print only body
-v, --verbose Verbose output
-q, --quiet Quieter output
-S, --stream Stream response body
-o, --output FILE Save output to file
-d, --download Download mode
--pretty STYLE Output processing
--style STYLE Output coloring style

Session Options

Option Description
-s, --session SESSION_NAME Use session
--session-read-only SESSION Use read-only session

Miscellaneous Options

Option Description
--help Show help
--version Show version
--traceback Show exception traceback
--debug Show debugging information

Response Processing

HTTPie offers tools for processing and filtering responses.

Using with jq

Combine HTTPie with jq for powerful JSON processing:

http api.example.com/users | jq '.[] | select(.active==true)'

Extracting Data for Scripts

Extract specific headers for scripting:

token=$(http -b POST api.example.com/auth username=admin password=secret | jq -r .token)
http api.example.com/secure "Authorization: Bearer $token"

Plugins and Extensions

HTTPie's functionality can be extended with plugins:

Plugin Description Installation
httpie-jwt-auth JWT authentication pip install httpie-jwt-auth
httpie-oauth OAuth support pip install httpie-oauth
httpie-aws-auth AWS authentication pip install httpie-aws-auth
httpie-unixsocket Unix socket support pip install httpie-unixsocket

Example: JWT Authentication

http --auth-type=jwt --auth=my_token api.example.com

HTTPie vs cURL

While both are HTTP clients, they have different strengths:

Feature HTTPie cURL
Syntax User-friendly Powerful but complex
Defaults Sensible defaults Requires explicit options
Output Colorized by default Raw by default
JSON handling Built-in Requires formatting
Learning curve Gentle Steeper
Availability Modern systems Nearly universal

Troubleshooting

Certificate Verification Errors

http --verify=no example.com

Debugging Requests

http -v example.com

Timeout Issues

http --timeout=60 example.com

Encoding Problems

http --print=Hb example.com

Conclusion

HTTPie has revolutionized command-line HTTP interactions with its intuitive syntax and beautiful output. Whether you're a developer testing APIs, a DevOps engineer automating infrastructure, or a system administrator troubleshooting services, HTTPie offers a more pleasant experience than traditional tools.

The combination of sensible defaults, powerful capabilities, and extensibility makes HTTPie an essential tool in any modern developer's toolkit. As web APIs continue to proliferate, having a tool that makes HTTP interactions more human-friendly becomes increasingly valuable.

Thank you for reading this comprehensive guide to HTTPie. We hope it helps you master this powerful tool and enhances your command-line productivity when working with HTTP services and APIs.