Skip to content

Getting Started

The basic usage is to create an HttpClient.

GET

The following example demonstrates a simple GET request.

import asyncio
from bareclient import HttpClient

async def main(url: str) -> None:
    async with HttpClient(url) as response:
        if response.status == 200 and response.body is not None:
            async for part in response.body:
                print(part)

asyncio.run(main('https://docs.python.org/3/library/cgi.html'))

The HttpClient request provides an async context yielding a response.

POST

The following code demonstrates a simple POST request.

import asyncio
import json
from bareutils import text_writer, response_code
from bareclient import HttpClient

async def main(url: str) -> None:
    obj = {'name': 'Rob'}
    body = json.dumps(obj)

    async with HttpClient(
            url,
            method='POST',
            headers=[(b'content-type', b'application/json')],
            body=text_writer(body)
    ) as response:
        if response_code.is_successful(response.status):
            print("OK")

asyncio.run(main('http://localhost:9009/test/api/info'))

Note that the body content is provided with the test_writer utility which turns the payload into an async iterator.