Advocate HTTP Client

1.0.0 · active · verified Thu Apr 16

Advocate is a Python library providing a safe wrapper around the popular `requests` library for making HTTP requests on behalf of a third party. It helps prevent common security pitfalls like SSRF by allowing developers to define strict URL validation patterns, limit redirects, set timeouts, and control request options. The current version is 1.0.0, and it maintains a stable release cadence focused on security and reliability.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `Advocate` with a strict URL regex pattern, disable redirects, set a timeout, and make a safe GET request. It also shows how the library prevents requests to URLs that don't match the configured pattern, raising `InvalidURLError`.

from advocate import Advocate

# Configure Advocate for safe third-party requests
# This example allows requests only to google.com/search
advocate = Advocate(
    url_regex_pattern="^https://www\.google\.com/search",
    max_redirects=0, # Disallow redirects for this sensitive operation
    raise_on_redirect=True,
    timeout=5, # Set a timeout to prevent hanging requests
    requests_options={
        "headers": {"User-Agent": "MySafeClient/1.0"},
        "verify": True # Ensure SSL verification is on
    }
)

try:
    # Make a safe GET request
    response = advocate.get("https://www.google.com/search?q=python+advocate")
    response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
    print(f"Request successful! Status: {response.status_code}")
    print("First 200 characters of response:")
    print(response.text[:200])
except advocate.exceptions.InvalidURLError as e:
    print(f"Invalid URL error: {e}")
except advocate.exceptions.RedirectError as e:
    print(f"Redirect error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

# Example of a disallowed URL (will raise InvalidURLError)
try:
    advocate.get("http://internal-api.example.com/sensitive-data")
except advocate.exceptions.InvalidURLError as e:
    print(f"Successfully blocked disallowed URL: {e}")
except Exception as e:
    print(f"Unexpected error for disallowed URL: {e}")

view raw JSON →