Skip to content

Send requests

A request has an HTTP method, a destination and, when the operation needs it, data. For example, to read a product from a catalog, send GET /products/42. To create one, send POST /products with the product details.

The functions below are independent. First, configure the catalog profile with your API’s origin as shown in your first request. For the example hostname https://catalog.example.com, use a test fixture. Copy the function for the method you need and call it with await from an Orionis controller, command or test. For example, products = await list_products() runs the GET example.

throw() raises HttpStatusException for 4xx and 5xx responses. The success statuses and documents below are part of the example catalog’s contract. To change the destination, authenticate, add headers, or send forms and small files, see request data. To handle a failure or redirect, see Responses and errors.

Read one page of the catalog. Use params= for filters, sorting or a page number supported by the provider. To retrieve a single product, use its path, /products/42.

from orionis.support.facades.http import Http
async def list_products():
result = await Http.client("catalog").get("/products", params={"page": "2"})
return result.throw().json("data")

This sends GET /products?page=2 with no body. If the catalog responds with 200 and {"data":[{"id":42,"name":"Notebook"}]}, the function returns the list [{"id":42,"name":"Notebook"}].

Create a product in the collection. Pass the document with json= so Orionis encodes it and sends Content-Type: application/json.

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

POST /products carries the three fields in its body. If the API responds with 201 Created and {"data":{"id":42}}, the function returns 42. Some APIs supply the new URL in the Location header, which you can read with result.header("Location").

Replace the representation of product 42. This API expects every writable field, including the ones you want to keep.

from orionis.support.facades.http import Http
async def replace_product():
product = {"sku": "NB-42", "name": "Large notebook", "stock": 12}
result = await Http.client("catalog").put("/products/42", json=product)
return result.throw().json("data.name")

PUT /products/42 sends the complete document. A 200 response with {"data":{"id":42,"name":"Large notebook"}} returns "Large notebook" from the function. If your provider confirms replacement with 204, read result.throw().status(). That status carries no body.

Update just the stock count. This catalog accepts JSON Merge Patch. Use the media type and patch format documented by your provider.

from orionis.support.facades.http import Http
async def update_stock():
result = await Http.client("catalog").patch(
"/products/42",
json={"stock": 5},
headers={"Content-Type": "application/merge-patch+json"},
)
return result.throw().json("data.stock")

PATCH /products/42 sends {"stock":5}. The provider preserves the other fields. If it returns 200 with {"data":{"stock":5}}, the function returns the integer 5.

Delete the product identified by the path. The catalog requires only the identifier for this operation.

from orionis.support.facades.http import Http
async def delete_product():
result = await Http.client("catalog").delete("/products/42")
return result.throw().status()

DELETE /products/42 is sent without a body. For this catalog, 204 No Content confirms deletion and the function returns 204. If your application treats a 404 as “already removed,” check result.status() == 404 before calling throw().

Check the product’s metadata without downloading its representation. An ETag, when the server provides one, identifies a version of that representation.

from orionis.support.facades.http import Http
async def product_etag():
result = await Http.client("catalog").head("/products/42")
return result.throw().header("ETag")

HEAD /products/42 sends no body. A 200 with ETag: "v3" returns the tag, including its quotes. header("ETag") returns None if the header is missing. Read the headers and status for HEAD. Its body() is b"".

Ask which operations the server allows on a product. For example, an administration tool can inspect Allow to find out whether the resource supports updates.

from orionis.support.facades.http import Http
async def product_capabilities():
result = await Http.client("catalog").options("/products/42")
return result.throw().header("Allow")

If OPTIONS /products/42 returns 204 and Allow: GET, HEAD, PUT, PATCH, DELETE, OPTIONS, the function returns that list of methods as a string. If the server omits the header, the result is None. Consult the provider’s contract to find out which operations are available.

Search with a document in the body when the provider supports the QUERY HTTP method. Orionis requires an explicit body. params= adds URL parameters and can be combined with json=, data= or content=. For bytes in content=, specify a concrete Content-Type accepted by the server. If the provider advertises search formats in the optional Accept-Query header, read them with result.headerValues("Accept-Query").

from orionis.support.facades.http import Http
async def search_products():
result = await Http.client("catalog").query(
"/products", params={"page": "2"}, json={"available": True}
)
return result.throw().json("data")

The call sends QUERY /products?page=2 with {"available":true} as its JSON body. A 200 and {"data":[42,43]} return [42, 43]. For 405, check whether the endpoint supports QUERY. For 415, check the media type. To test this call, register the QUERY method and final URL in the fixture. Testing the client

Use send(method, url, **options) when the method is selected at runtime. It accepts the eight methods above and the same request options.

from orionis.support.facades.http import Http
async def inspect_product(metadata_only: bool = False):
method = "HEAD" if metadata_only else "GET"
result = await Http.client("catalog").send(method, "/products/42")
result.throw()
if metadata_only:
return result.header("ETag")
return result.json("data.name")

await inspect_product() sends GET /products/42 and reads the name from a document such as {"data":{"name":"Notebook"}}. await inspect_product(metadata_only=True) sends HEAD to the same path and returns the advertised tag. Either call makes one attempt.