Skip to content

Getting Started

Installation

bash
pip install zapros

Basic Request

python
import asyncio
from zapros import AsyncClient


async def main():
    async with AsyncClient() as client:
        response = await client.request(
            "GET",
            "https://httpbin.org/get",
        )
        print(response.status)
        print(response.headers["content-type"])
        print(await response.atext())


asyncio.run(main())
python
from zapros import Client

with Client() as client:
    response = client.request(
        "GET",
        "https://httpbin.org/get",
    )
    print(response.status)
    print(response.headers["content-type"])
    print(response.text())

Query Parameters

python
response = await client.request(
    "GET",
    "https://httpbin.org/get",
    params={"page": "1", "limit": "10"},
)
python
response = client.request(
    "GET",
    "https://httpbin.org/get",
    params={"page": "1", "limit": "10"},
)

Request Body

JSON

python
response = await client.request(
    "POST",
    "https://httpbin.org/post",
    json={"key": "value"},
)
python
response = client.request(
    "POST",
    "https://httpbin.org/post",
    json={"key": "value"},
)

Form (URL-encoded)

python
response = await client.request(
    "POST",
    "https://httpbin.org/post",
    form={
        "username": "alice",
        "password": "secret",
    },
)
python
response = client.request(
    "POST",
    "https://httpbin.org/post",
    form={
        "username": "alice",
        "password": "secret",
    },
)

Raw bytes

python
response = await client.request(
    "POST",
    "https://httpbin.org/post",
    body=b"raw bytes here",
)
python
response = client.request(
    "POST",
    "https://httpbin.org/post",
    body=b"raw bytes here",
)

Custom Headers

python
response = await client.request(
    "GET",
    "https://httpbin.org/get",
    headers={"Authorization": "Bearer my-token"},
)
python
response = client.request(
    "GET",
    "https://httpbin.org/get",
    headers={"Authorization": "Bearer my-token"},
)

Multipart File Upload

python
from zapros import (
    AsyncClient,
    Multipart,
    Part,
)

file_part = (
    Part
    .bytes(b"file content")
    .file_name("hello.txt")
    .mime_type("text/plain")
)
multipart = (
    Multipart()
    .text("field", "value")
    .part("file", file_part)
)

async with AsyncClient() as client:
    response = await client.request(
        "POST",
        "https://httpbin.org/post",
        multipart=multipart,
    )
python
from zapros import (
    Client,
    Multipart,
    Part,
)

file_part = (
    Part
    .bytes(b"file content")
    .file_name("hello.txt")
    .mime_type("text/plain")
)
multipart = (
    Multipart()
    .text("field", "value")
    .part("file", file_part)
)

with Client() as client:
    response = client.request(
        "POST",
        "https://httpbin.org/post",
        multipart=multipart,
    )

Streaming Responses

Use stream() to process the response body incrementally without loading it all into memory.

python
async with AsyncClient() as client:
    async with client.stream(
        "GET",
        "https://httpbin.org/stream/10",
    ) as response:
        async for chunk in response.async_iter_bytes():
            process(chunk)
python
from zapros import Client

with Client() as client:
    with client.stream(
        "GET",
        "https://httpbin.org/stream/10",
    ) as response:
        for chunk in response.iter_bytes():
            process(chunk)