HTTPie

3.2.4 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates both the recommended way of using HTTPie programmatically (as a subprocess) and the experimental direct `call_main` API. The subprocess method provides stability and mimics standard CLI usage. Remember to set `HTTP_AUTH_TOKEN` in your environment for the auth example.

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

view raw JSON →