pythonhttpserveruvloop

How to get the Body from a request, using httptools?


How to get request body with content-type: Application/json using python httptools? I`m using it with uvloop.

The request class looks like this:

class HttpRequest:
    __slots__ = ('_protocol', '_url', '_headers', '_version')

    def __init__(self, protocol, url, headers, version):
        self._protocol = protocol
        self._url = url
        self._headers = headers
        self._version = version

Solution

  • You can add a method named on_body that will receive the body of the HTTP request as its parameter.

    The supported callback functions on the object you provide when creating a HttpRequestParser with httptools are:

    - on_message_begin()
    - on_header(name: bytes, value: bytes)
    - on_headers_complete()
    - on_body(body: bytes)
    - on_message_complete()
    - on_chunk_header()
    - on_chunk_complete()