Skip to content

Parameters, request bodies and authentication

The examples use the catalog profile from your first request. It allows https://catalog.example.com. To use your provider, change the configured origin and use paths supported by its API. Add this import to the file containing your example. Call the functions from a controller, command or other async entry point in a running application.

from orionis.support.facades import Http
async def product_by_url():
result = await Http.get("https://catalog.example.com/products/42")
return result.throw().json()

For a one-off call, pass the full HTTPS URL. Orionis checks the origin against allowed_origins and uses the global limits.

async def product_on_origin():
catalog = Http.baseUrl("https://catalog.example.com")
result = await catalog.get("/products/42")
return result.throw().json()

baseUrl() takes the HTTPS origin and, when applicable, its port. Put an API prefix in the path, for example /v2/products/42. Reuse catalog for other resources on that origin.

async def product_by_profile():
catalog = Http.client("catalog")
result = await catalog.get("/products/42")
return result.throw().json()

client("catalog") loads the origin, headers and budgets from its profile. Once you select a profile or bind an origin, start each path with a single slash. Orionis rejects products/42, //another-host/products and absolute URLs on that chain.

async def list_products():
result = await Http.client("catalog").get(
"/products", params={"page": "2", "sort": "name"}
)
return result.throw().json("data")

This sends GET /products?page=2&sort=name. For unique keys, pass a dictionary of strings in params. Convert numbers, dates and booleans to the text representation your provider expects.

async def filter_products():
result = await Http.client("catalog").get(
"/products?page=2", params=[("tag", "home office"), ("tag", "café")]
)
return result.throw().json("data")

The URL is /products?page=2&tag=home+office&tag=caf%C3%A9. Pairs preserve order, allow repeated keys and are appended to parameters already present in the URL. Pass the original text for Orionis to encode once. The final URL can be up to 8192 bytes. Combine params= with a body when the method allows one. Choose a method

async def create_product():
catalog = Http.client("catalog").acceptJson()
result = await catalog.post(
"/products", json={"sku": "NB-42", "name": "Notebook", "stock": 8}
)
return result.throw().json("data.id")

json= serializes the object as UTF-8 and adds Content-Type: application/json. acceptJson() sends Accept: application/json to request JSON in the response. This example expects an object such as {"data":{"id":42}} from the API.

async def change_stock():
result = await Http.client("catalog").patch(
"/products/42",
json={"stock": 4},
headers={"Content-Type": "application/merge-patch+json"},
)
return result.throw().status()

If the endpoint requires a specific JSON media type, pass it in headers=. The encoder accepts application/json and application/…+json. A Content-Type: text/plain or an explicit charset other than UTF-8 with json= raises HttpRequestException before sending.

Supported values are dictionaries with string keys, lists, strings, finite numbers, booleans and None. Convert dates, decimals and application models to those types. Alongside the profile’s byte limit, the client checks nesting depth (64 levels) and the number of visited values (65,536).

Omit the body, send JSON null or send zero bytes

Section titled “Omit the body, send JSON null or send zero bytes”
async def trigger_refresh():
result = await Http.client("catalog").post("/refresh")
return result.throw().status()
async def unset_preference():
result = await Http.client("catalog").put("/preferences/default", json=None)
return result.throw().status()
async def clear_note():
result = await Http.client("catalog").put(
"/products/42/note", content=b"", headers={"Content-Type": "text/plain"}
)
return result.throw().status()

The first function requests an action without supplying a body. The second sends the four bytes of null with a JSON media type. The third explicitly supplies zero bytes as plain text. An omitted body and an empty body can have the same wire length. Choose the representation your provider accepts. Supply one body argument per send. GET and HEAD reject explicit bodies. QUERY requires an explicit body and a concrete media type.

async def submit_form():
result = await Http.client("catalog").post(
"/subscriptions", data={"email": "reader@example.com", "list": "stock alerts"}
)
return result.throw().status()

data= encodes the form as application/x-www-form-urlencoded. Here the body is email=reader%40example.com&list=stock+alerts. Both dictionary keys and values are strings.

async def replace_tags():
form = Http.client("catalog").asForm()
result = await form.put("/products/42/tags", data=[("tag", "new"), ("tag", "sale")])
return result.throw().status()

asForm() reserves that chain for form data. The pairs send tag=new&tag=sale in order. Using json= or content= on an asForm() chain raises HttpRequestException. If you set the form media type explicitly, use application/x-www-form-urlencoded and UTF-8 if you specify a charset.

Send text, upload bytes and download files

Section titled “Send text, upload bytes and download files”
async def save_note(text: str):
result = await Http.client("catalog").put(
"/products/42/note",
content=text.encode("utf-8"),
headers={"Content-Type": "text/plain; charset=utf-8"},
)
return result.throw().status()

content= sends a bytes object as supplied. For text, choose an encoding and declare that encoding to the service. For XML, for example, encode the document and use the provider’s required media type.

async def upload_label(pdf: bytes):
result = await Http.client("catalog").put(
"/products/42/label", content=pdf, headers={"Content-Type": "application/pdf"}
)
return result.throw().status()
async def download_label() -> bytes:
result = await Http.client("catalog").get(
"/products/42/label", headers={"Accept": "application/pdf"}
)
result.throw()
media = (result.header("Content-Type") or "").split(";", 1)[0].strip().lower()
if result.status() != 200 or media != "application/pdf":
raise ValueError("Expected a PDF document")
return result.body()

If you have a PDF as bytes, upload_label sends it as the entire body. download_label checks the status and media type before returning the bytes. Keep uploads and downloads within the profile’s request and response budgets. Version 1.10.0 buffers the full body in memory. Its public API has no multipart encoder (files=) or streaming download iterator. For a size-limited read from disk, see integration recipes.

Share headers and override them for one request

Section titled “Share headers and override them for one request”
async def translated_products():
english = Http.client("catalog").acceptJson().withHeaders({"Accept-Language": "en"})
spanish = await english.get("/products/42", headers={"accept-language": "es"})
original = await english.get("/products/42")
return spanish.throw().json("data.name"), original.throw().json("data.name")

The first call sends Accept-Language: es and the second sends Accept-Language: en. Both request JSON and read the data.name field from the catalog response. Precedence runs from profile headers, to chain methods, to per-call headers=. Header names are case-insensitive. Each builder such as withHeaders() returns an independent chain, so you can reuse the original.

Use valid HTTP field names and printable ASCII string values without leading or trailing spaces. Before an origin is bound, withHeaders() accepts Accept and Content-Type. The combined header budget is 16 KiB, including generated fields. Reserved names are Authorization, Cookie, Cookie2, Host, Content-Length, Transfer-Encoding, Connection, Keep-Alive, Proxy-Authorization, Proxy-Authenticate, Proxy-Connection, TE, Trailer, Upgrade, Accept-Encoding and Expect.

Select the profile or bind an origin with baseUrl() before attaching credentials. Obtain secrets at runtime, for example from your application’s secret store. Headers declared in profile configuration are included in the configuration cache.

async def private_product(token: str):
catalog = Http.client("catalog").withToken(token)
result = await catalog.get("/products/42")
return result.throw().json()

withToken builds Authorization: Bearer …. Pass only the token value from your provider, as printable ASCII without spaces.

async def supplier_product(username: str, password: str):
catalog = Http.client("catalog").withBasicAuth(username, password)
result = await catalog.get("/products/42")
return result.throw().json()

Orionis encodes username:password as UTF-8 and Base64 and builds Authorization: Basic …. The username must be free of colons. Supply both credentials as printable text.

async def partner_product(api_key: str):
catalog = Http.client("catalog").withHeaders({"X-API-Key": api_key})
result = await catalog.get("/products/42")
return result.throw().json()

Use the field name specified by the provider. The key belongs to the derived chain and its requests. Keep secrets out of URLs and authentication headers out of logs.

async def check_stock():
catalog = Http.client("catalog").timeout(2).connectTimeout(1).deadline(4)
result = await catalog.get("/products/42/stock")
return result.throw().json("stock")

Values are positive seconds. timeout(2) applies up to 2 s to connecting, reading, writing and waiting for a pool connection, respecting each phase ceiling. connectTimeout(1) then sets the connect phase to 1 s. deadline(4) caps the complete operation, including waiting, at 4 s. The latter two setters reject values above their profile ceilings. See concurrency and cancellation and examples for each HTTP method.