Smithy HTTP

0.4.0 · active · verified Fri Apr 17

Smithy-http provides foundational HTTP components and primitives for Smithy tooling in Python. It defines standard interfaces for HTTP requests, responses, headers, and middleware, ensuring compliance with Smithy's protocol specifications. This library is primarily a building block for Smithy-generated Python clients and other Smithy-compliant services. The current version is 0.4.0, and releases are generally tied to the development cadence of the broader `smithy-python` project.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to construct basic `HttpRequest` and `HttpResponse` objects using `smithy-http`'s core components. It highlights how to set the HTTP method, URL, headers, and body for both request and response entities. Note that `smithy-http` does not perform actual network calls; it provides the data structures for them.

from smithy_http.request import HttpRequest
from smithy_http.response import HttpResponse
from smithy_http.headers import Headers
from io import BytesIO

# --- Construct an HttpRequest ---
url = "https://example.com/path"
method = "POST"
headers = Headers({"Content-Type": "application/json", "Accept": "application/json"})
body_content = b'{"key": "value"}'
body = BytesIO(body_content)

request = HttpRequest(
    method=method,
    url=url,
    headers=headers,
    body=body
)

print(f"Request Method: {request.method}")
print(f"Request URL: {request.url}")
print(f"Request Headers: {dict(request.headers)}")
request.body.seek(0)
print(f"Request Body: {request.body.read().decode('utf-8')}")

# --- Construct an HttpResponse ---
status_code = 200
response_headers = Headers({"Content-Type": "application/json"})
response_body_content = b'{"message": "success"}'
response_body = BytesIO(response_body_content)

response = HttpResponse(
    status_code=status_code,
    headers=response_headers,
    body=response_body
)

print(f"\nResponse Status Code: {response.status_code}")
print(f"Response Headers: {dict(response.headers)}")
response.body.seek(0)
print(f"Response Body: {response.body.read().decode('utf-8')}")

view raw JSON →