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 fetchUnlike 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 stringPOST 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| Parameter | Type | Description |
|---|---|---|
url | str | The URL to fetch |
headers | dict | Optional request headers |
fetch.post
fetch.post(url, body=None, headers=None) -> dict| Parameter | Type | Description |
|---|---|---|
url | str | The URL to post to |
body | str/bytes | Request body |
headers | dict | Optional request headers |
fetch.request
fetch.request(method, url, body=None, headers=None) -> dict| Parameter | Type | Description |
|---|---|---|
method | str | HTTP method (GET, POST, PUT, DELETE, etc.) |
url | str | The URL |
body | str/bytes | Request body |
headers | dict | Optional 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")