ucharm

fetch

HTTP/HTTPS client with built-in TLS support

fetch

The fetch module provides a simple HTTP/HTTPS client with built-in TLS support.

import fetch

Unlike Python's requests library, fetch is built into ucharm with zero dependencies. TLS is handled natively via BearSSL.

GET Requests

response = fetch.get("https://api.github.com/users/octocat")

print(response["status"])       # 200
print(response["body"].decode()) # JSON string

POST Requests

import json

data = json.dumps({"name": "ucharm", "version": "1.0"})
response = fetch.post(
    "https://httpbin.org/post",
    body=data,
    headers={"Content-Type": "application/json"}
)

print(response["status"])
result = json.loads(response["body"].decode())

Generic Request

For other HTTP methods (PUT, DELETE, PATCH, etc.):

response = fetch.request(
    "PUT",
    "https://api.example.com/resource/123",
    body='{"updated": true}',
    headers={"Content-Type": "application/json"}
)

Response Format

All fetch functions return a dictionary:

{
    "status": 200,              # HTTP status code
    "headers": {...},           # Response headers (dict)
    "body": b"...",             # Response body (bytes)
}

Function Reference

fetch.get

fetch.get(url, headers=None) -> dict
ParameterTypeDescription
urlstrThe URL to fetch
headersdictOptional request headers

fetch.post

fetch.post(url, body=None, headers=None) -> dict
ParameterTypeDescription
urlstrThe URL to post to
bodystr/bytesRequest body
headersdictOptional request headers

fetch.request

fetch.request(method, url, body=None, headers=None) -> dict
ParameterTypeDescription
methodstrHTTP method (GET, POST, PUT, DELETE, etc.)
urlstrThe URL
bodystr/bytesRequest body
headersdictOptional request headers

Example: JSON API Client

import fetch
import json

def api_get(endpoint):
    url = f"https://jsonplaceholder.typicode.com{endpoint}"
    response = fetch.get(url)
    if response["status"] == 200:
        return json.loads(response["body"].decode())
    return None

def api_post(endpoint, data):
    url = f"https://jsonplaceholder.typicode.com{endpoint}"
    response = fetch.post(
        url,
        body=json.dumps(data),
        headers={"Content-Type": "application/json"}
    )
    return json.loads(response["body"].decode())

# Get a user
user = api_get("/users/1")
print(f"Name: {user['name']}")

# Create a post
new_post = api_post("/posts", {
    "title": "Hello",
    "body": "World",
    "userId": 1
})
print(f"Created post ID: {new_post['id']}")

Example: Download File

import fetch

def download(url, filename):
    response = fetch.get(url)
    if response["status"] == 200:
        with open(filename, "wb") as f:
            f.write(response["body"])
        return True
    return False

if download("https://example.com/file.zip", "file.zip"):
    print("Downloaded!")

Example: With Error Handling

import fetch
import json
import charm

def safe_fetch(url):
    try:
        response = fetch.get(url)
        if response["status"] >= 400:
            charm.error(f"HTTP {response['status']}")
            return None
        return response
    except Exception as e:
        charm.error(f"Request failed: {e}")
        return None

response = safe_fetch("https://api.github.com/users/octocat")
if response:
    data = json.loads(response["body"].decode())
    charm.success(f"Found user: {data['login']}")

HTTPS Support

HTTPS is fully supported with built-in TLS via BearSSL. Certificate verification is enabled by default.

# Works out of the box
response = fetch.get("https://secure-api.example.com/data")

On this page