Multipart

class Multipart(**kwargs)

Represents a multipart HTTP message body, parsed according to the syntax of RFC 2046.

Of particular interest to HTTP are multipart/byte-ranges and multipart/form-data,

Although the headers of a Multipart body part will contain the full headers from that body part, libsoup does not interpret them according to MIME rules. For example, each body part is assumed to have “binary” Content-Transfer-Encoding, even if its headers explicitly state otherwise. In other words, don’t try to use Multipart for handling real MIME multiparts.

Constructors

class Multipart
classmethod new(mime_type: str) Multipart

Creates a new empty Multipart with a randomly-generated boundary string.

Note that mime_type must be the full MIME type, including “multipart/”.

See also: new_from_multipart.

Parameters:

mime_type – the MIME type of the multipart to create.

classmethod new_from_message(headers: MessageHeaders, body: Bytes) Multipart | None

Parses headers and body to form a new Multipart

Parameters:
  • headers – the headers of the HTTP message to parse

  • body – the body of the HTTP message to parse

Methods

class Multipart
append_form_file(control_name: str, filename: str | None, content_type: str | None, body: Bytes) None

Adds a new MIME part containing body to multipart

Uses “Content-Disposition: form-data”, as per the HTML forms specification.

Parameters:
  • control_name – the name of the control associated with this file

  • filename – the name of the file, or None if not known

  • content_type – the MIME type of the file, or None if not known

  • body – the file data

append_form_string(control_name: str, data: str) None

Adds a new MIME part containing data to multipart.

Uses “Content-Disposition: form-data”, as per the HTML forms specification.

Parameters:
  • control_name – the name of the control associated with data

  • data – the body data

append_part(headers: MessageHeaders, body: Bytes) None

Adds a new MIME part to multipart with the given headers and body.

(The multipart will make its own copies of headers and body, so you should free your copies if you are not using them for anything else.)

Parameters:
  • headers – the MIME part headers

  • body – the MIME part body

free() None

Frees multipart.

get_length() int

Gets the number of body parts in multipart.

get_part(part: int) tuple[bool, MessageHeaders, Bytes]

Gets the indicated body part from multipart.

Parameters:

part – the part number to get (counting from 0)

to_message(dest_headers: MessageHeaders) Bytes

Serializes multipart to dest_headers and dest_body.

Parameters:

dest_headers – the headers of the HTTP message to serialize multipart to