HTTPie
HTTPie is a modern, user-friendly command-line HTTP client for the API era, designed to make API interaction intuitive and efficient. The current version is 3.2.4, and it maintains an active release cadence with frequent patch and minor updates.
Warnings
- breaking HTTPie v3.0.0 and later dropped support for Python 3.6. Users on older Python versions must upgrade Python or stick to HTTPie versions < 3.0.0.
- gotcha The `httpie.core` programmatic API (e.g., `call_main`) is not officially stable and is subject to change between versions without a dedicated deprecation cycle. Direct programmatic interaction should be used with caution.
- gotcha HTTPie has experienced issues with SSL connections and compatibility with underlying `requests` and `urllib3` versions in the past (e.g., 3.2.2, 3.2.3, 3.2.4 patches). While often resolved quickly, this indicates sensitivity to its core dependencies.
- deprecated HTTPie v3.1.0 fixed a critical security vulnerability related to cookie exposure on redirects to third-party hosts. While fixed, relying on older versions could expose users to this vulnerability.
Install
-
pip install httpie
Imports
- call_main
from httpie.core import call_main
Quickstart
import subprocess
from httpie.core import call_main
# Recommended: Run HTTPie as a subprocess
print("--- Using subprocess (recommended) ---")
try:
result = subprocess.run(['http', 'https://httpbin.org/get', 'X-Auth-Token: ' + os.environ.get('HTTP_AUTH_TOKEN', 'none')], capture_output=True, text=True, check=True)
print(result.stdout)
except subprocess.CalledProcessError as e:
print(f"Subprocess failed: {e}\n{e.stderr}")
# Alternative: Use the internal call_main (API not guaranteed stable)
print("\n--- Using httpie.core.call_main (experimental) ---")
# Redirect stdout/stderr to capture output, as call_main prints directly
import io
import sys
old_stdout = sys.stdout
redirected_output = io.StringIO()
sys.stdout = redirected_output
try:
# call_main expects a list of CLI arguments
args = ['https://httpbin.org/post', 'hello=world', 'Content-Type: application/json']
status_code = call_main(args)
print(f"Call main exited with status: {status_code}")
print(redirected_output.getvalue())
finally:
sys.stdout = old_stdout # Restore stdout
# Example of setting an environment variable for a subprocess call
import os
os.environ['HTTP_TEST_VAR'] = 'test_value'
print("\n--- Subprocess with environment variable ---")
try:
result_env = subprocess.run(['http', 'https://httpbin.org/get', 'HTTP_TEST_VAR'], capture_output=True, text=True, check=True)
print(result_env.stdout)
except subprocess.CalledProcessError as e:
print(f"Subprocess failed: {e}\n{e.stderr}")
del os.environ['HTTP_TEST_VAR'] # Clean up