Skip to content

decode_message

faststream.broker.parsers.decode_message #

decode_message(
    message: StreamMessage[Any],
) -> DecodedMessage

Decodes a message.

PARAMETER DESCRIPTION
message

The message to decode.

TYPE: StreamMessage[Any]

RETURNS DESCRIPTION
DecodedMessage

The decoded message.

RAISES DESCRIPTION
JSONDecodeError

If the message body cannot be decoded as JSON.

Note

The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)

Source code in faststream/broker/parsers.py
def decode_message(message: StreamMessage[Any]) -> DecodedMessage:
    """Decodes a message.

    Args:
        message: The message to decode.

    Returns:
        The decoded message.

    Raises:
        JSONDecodeError: If the message body cannot be decoded as JSON.
    !!! note

        The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)
    """
    body = message.body
    m: DecodedMessage = body

    if message.content_type:
        if ContentTypes.text.value in message.content_type:
            m = body.decode()
        elif ContentTypes.json.value in message.content_type:  # pragma: no branch
            m = json.loads(body)

    else:
        with suppress(json.JSONDecodeError):
            m = json.loads(body)

    return m

Last update: 2023-11-13