Skip to content

HTTP Methods

FastHTTP supports all main HTTP methods.

GET - Retrieve Data

from fasthttp import FastHTTP
from fasthttp.response import Response

app = FastHTTP()


@app.get(url="https://api.example.com/users")
async def get_users(resp: Response) -> dict:
    return resp.json()

POST - Create Data

@app.post(
    url="https://api.example.com/users",
    json={"name": "John", "email": "john@example.com"}
)
async def create_user(resp: Response) -> dict:
    return resp.json()

PUT - Full Update

@app.put(
    url="https://api.example.com/users/1",
    json={"name": "Jane", "email": "jane@example.com"}
)
async def update_user(resp: Response) -> dict:
    return resp.json()

PATCH - Partial Update

@app.patch(
    url="https://api.example.com/users/1",
    json={"age": 25}
)
async def patch_user(resp: Response) -> dict:
    return resp.json()

QUERY - Complex Reads with a Body

QUERY is a newer HTTP method (proposed in an IETF draft, draft-ietf-httpbis-safe-method-w-body) for read operations that don't fit in a URL. It's safe and idempotent like GET, but carries a JSON body like POST — useful for complex search/filter requests.

@app.query(
    url="https://api.example.com/users/search",
    json={"role": "admin", "active": True}
)
async def search_users(resp: Response) -> dict:
    return resp.json()

Note

QUERY is not yet universally supported by servers and proxies — only use it against APIs that explicitly document QUERY support. It also isn't part of the standard OpenAPI Path Item Object method set, so QUERY routes won't be rendered in Swagger UI (see OpenAPI), even though they're included in the generated /openapi.json.

DELETE - Remove Data

@app.delete(url="https://api.example.com/users/1")
async def delete_user(resp: Response) -> dict:
    return resp.status

HEAD - Check Endpoint

Returns only headers, no body. Useful for checking if a resource exists or inspecting metadata.

@app.head(url="https://api.example.com/users")
async def check_users(resp: Response) -> int:
    return resp.status

OPTIONS - Allowed Methods

Returns the HTTP methods supported by the endpoint.

@app.options(url="https://api.example.com/users")
async def allowed_methods(resp: Response) -> dict:
    return {"allow": resp.headers.get("allow", "")}

Decorator Parameters

Parameter Description
url Request URL (required)
params Query parameters
json JSON body (for POST, PUT, PATCH, QUERY)
data Raw bytes body
files File uploads (multipart/form-data)
tags Tags for grouping
dependencies List of dependencies
response_model Pydantic model for validation
request_model Pydantic model for request validation
responses Pydantic models for error responses

Return Values

Handlers can return different types:

# Return dict - converted to JSON
async def return_dict(resp: Response) -> dict:
    return {"message": "Hello"}

# Return list
async def return_list(resp: Response) -> list:
    return [1, 2, 3]

# Return string
async def return_string(resp: Response) -> str:
    return "Hello, World!"

# Return number (status code)
async def return_number(resp: Response) -> int:
    return resp.status

# Return Response object
async def return_response(resp: Response) -> Response:
    return resp