1
Modules HTTP Client
Logan Fick edited this page 2026-02-08 11:10:13 -05:00

Modules - HTTP Client

A shared HTTP client is available through ctx.http for making outgoing web requests. It wraps an aiohttp.ClientSession with convenience methods, a shared connection pool across all modules, and automatic lifecycle management.

Making Requests

All methods return an aiohttp async context manager. Use them with async with:

from owlbot.api import CommandContext, on_command

@on_command("weather")
async def weather(ctx: CommandContext) -> None:
    city = ctx.args.strip() or "London"
    url = f"https://wttr.in/{city}?format=3"

    async with ctx.http.get(url) as resp:
        if resp.status == 200:
            text = await resp.text()
            await ctx.owncast_client.send_message(text.strip())
        else:
            await ctx.owncast_client.send_message(f"Could not fetch weather (HTTP {resp.status})")

POST, PUT, and DELETE follow the same pattern:

async with ctx.http.post(webhook_url, json={"content": "Hello!"}) as resp:
    if resp.status >= 400:
        ctx.logger.error(f"Webhook failed: {resp.status}")

Available Methods

All methods pass through **kwargs to the underlying aiohttp.ClientSession method, so any parameter aiohttp supports is available.

Method Description
ctx.http.get(url, **kwargs) GET request.
ctx.http.post(url, **kwargs) POST request.
ctx.http.put(url, **kwargs) PUT request.
ctx.http.delete(url, **kwargs) DELETE request.
ctx.http.request(method, url, **kwargs) Request with any HTTP method.

Common kwargs: json=, data=, headers=, params=, timeout=.

Raw Session Access

If the convenience methods don't cover a specific need, the underlying aiohttp.ClientSession is accessible directly:

session = ctx.http.session

This provides the full aiohttp API (WebSockets, streaming responses, custom auth, etc.). The session should not be closed manually; its lifecycle is managed automatically.