GitHub

ServerRequest

API reference for ServerRequest.

A request the server is handling.

Attributes

AttributeTypeDescription
sourceobject

What the request was made from (an HttpClientRequest, the live server's connection state, an ASGI scope).

original_urlstr

The URL as received.

methodstr

The uppercase method as received, maybe outside HttpMethod.

streamStream[bytes, HttpServerError]

The body as it arrives.

bytes_Effect[bytes, HttpServerError]

The whole body, read once (Effect's arrayBuffer); past MaxBodySize it fails as BodyTooLarge.

urlstr

The path and query (the router may have removed a prefix).

headersHeaders
remote_addressstr | None

The client's address (behind proxies, as they report it).

peer_addressstr | None

The address of the connection's other end.

protocolRequestProtocol | None
cookiesMapping[str, str]
upgradeEffect[SocketService, HttpServerError]
textEffect[str, HttpServerError]

The body as UTF-8 (invalid bytes become U+FFFD).

jsonEffect[Json, HttpServerError]

The body parsed as JSON.

url_params_bodyEffect[UrlParams, HttpServerError]
multipart_streamStream[Part, Any]

The body's multipart parts, as they arrive.

multipartEffect[Persisted, Any, Scope | FileSystem | Path]

The body's multipart form, its files written to a scoped temporary directory (once).

Functions

__init__

__init__(url=None, headers=None, remote_address=None, peer_address=None, protocol=None) -> None
ParameterTypeDescription
urlstr | None = None
headersHeaders | None = None
remote_addressstr | None = None
peer_addressstr | None = None
protocolRequestProtocol | None = None
def __init__(
    self,
    url: str | None = None,
    headers: Headers | None = None,
    remote_address: str | None = None,
    peer_address: str | None = None,
    protocol: RequestProtocol | None = None,
) -> None:
    self._url: str | None = url
    self._headers: Headers | None = headers
    self._remote_address: str | None = remote_address
    self._peer_address: str | None = peer_address
    self._protocol: RequestProtocol | None = protocol

upgrade_with

upgrade_with(*, headers=None) -> Effect[SocketService, HttpServerError]

Upgrade the connection to a WebSocket, answering with headers too; a request that cannot be upgraded fails.

ParameterTypeDescription
headersHeadersInput | None = None

Returns Effect[SocketService, HttpServerError]

def upgrade_with(
    self, *, headers: HeadersInput | None = None
) -> Effect[SocketService, HttpServerError]:
    """Upgrade the connection to a WebSocket, answering with ``headers``
    too; a request that cannot be upgraded fails."""
    del headers
    return fail(request_parse_error(self, "Not an upgradeable ServerRequest"))

modify

modify(*, url=None, headers=None, remote_address=_UNSET, peer_address=None, protocol=None) -> ServerRequest

The request with these parts replaced (remote_address=None removes it); its body is this request's.

ParameterTypeDescription
urlstr | None = None
headersHeaders | None = None
remote_addressstr | _Unset | None = _UNSET
peer_addressstr | None = None
protocolRequestProtocol | None = None

Returns ServerRequest

def modify(
    self,
    *,
    url: str | None = None,
    headers: Headers | None = None,
    remote_address: str | _Unset | None = _UNSET,
    peer_address: str | None = None,
    protocol: RequestProtocol | None = None,
) -> ServerRequest:
    """The request with these parts replaced (``remote_address=None``
    removes it); its body is this request's."""
    copy = self._copy()
    copy._url = self.url if url is None else url
    copy._headers = self._headers if headers is None else headers
    copy._remote_address = (
        self._remote_address if isinstance(remote_address, _Unset) else remote_address
    )
    copy._peer_address = self._peer_address if peer_address is None else peer_address
    copy._protocol = self._protocol if protocol is None else protocol
    return copy

to_json

to_json() -> dict[str, object]

Returns dict[str, object]

def to_json(self) -> dict[str, object]:
    from pyfect.http.url_params import redact_url

    return {
        "_id": "HttpServerRequest",
        "method": self.method,
        "url": redact_url(self.original_url),
        "headers": self.headers.to_json(),
        "remoteAddress": self.remote_address,
    }