Adafruit CircuitPython Requests

4.1.16 · active · verified Fri Apr 17

Adafruit CircuitPython Requests provides a requests-like API for making HTTP requests from CircuitPython microcontrollers. It offers a familiar interface for GET, POST, PUT, DELETE, and HEAD operations, allowing microcontrollers with network capabilities (like WiFi or Ethernet) to interact with web services. The current version is 4.1.16, and it typically follows a rapid release cadence with bug fixes and minor improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the `adafruit_requests.Session` API. Note that actual network operations on CircuitPython require configuring `wifi.radio` (or Ethernet) and creating a `socketpool.SocketPool` instance, which involves specific hardware and `secrets.py` setup not fully runnable in a generic Python environment. The provided code includes mock objects to illustrate the API without requiring actual hardware, but real network requests will fail.

import ssl
import socketpool # Required for network communication
import wifi # Required for WiFi connectivity
import adafruit_requests

# --- This section requires CircuitPython board setup ---
# In a real CircuitPython environment, you'd connect to WiFi:
# try:
#     from secrets import secrets
# except ImportError:
#     print("WiFi secrets are kept in secrets.py, please add them there!")
#     raise
# wifi.radio.connect(secrets["ssid"], secrets["password"])
# pool = socketpool.SocketPool(wifi.radio)

# --- For demonstration purposes (not runnable without actual network setup) ---
# Mock objects for local run-ability, replace with real ones on device:
class MockRadio:
    ipv4_address = "192.168.1.100"
class MockSocketPool:
    def __init__(self, radio):
        pass
    # In a real scenario, this would create actual sockets
    def socket(self, family, type, proto=0):
        raise NotImplementedError("Mock socketpool does not provide real sockets")
class MockSSLContext:
    def __init__(self):
        pass
# Replace these mocks with real objects on a CircuitPython board
wifi.radio = MockRadio()
pool = MockSocketPool(wifi.radio)
ssl_context = MockSSLContext()

# Initialize the requests session with a socket pool and SSL context
# On a real CircuitPython device: requests = adafruit_requests.Session(pool, ssl.create_default_context())
requests = adafruit_requests.Session(pool, ssl_context)

# Make a GET request (this will fail in mock, but demonstrates API)
try:
    response = requests.get("http://example.com/api/data")
    print(f"Status Code: {response.status_code}")
    print(f"Response Text: {response.text}")
    response.close()
except Exception as e:
    print(f"Error making request (expected in mock setup): {e}")

# Make a POST request with JSON data
post_data = {"key": "value"}
try:
    response = requests.post("http://example.com/api/submit", json=post_data)
    print(f"POST Status Code: {response.status_code}")
    print(f"POST Response Text: {response.text}")
    response.close()
except Exception as e:
    print(f"Error making POST request (expected in mock setup): {e}")

view raw JSON →