SOAP attachments

Send and receive documents, images and scans as MTOM attachments, using plain Python bytes.

MTOM (Message Transmission Optimization Mechanism) transmits binary values, such as documents and images, as MIME parts instead of inline base64 text. Each value in the envelope is replaced with an xop:Include reference to its part. The IHE document submission and retrieval profiles require MTOM.

MTOM is enabled with the MTOM checkbox on the SOAP tab of an outgoing connection, and binary values are assigned as Python bytes.

Send binary content

Assign bytes to any field of the message. With MTOM on, each such value becomes a MIME part on the wire and the element contains an xop:Include reference to it:

request = SOAPMessage()
request.documentId = 'doc-123'
request.document = document_bytes

response = self.soap['Document Repository'].invoke('ProvideAndRegisterDocumentSet', request)

With MTOM off, the same bytes value is written inline as base64 - the service code does not change either way, only the wire format does.

Receive binary content

Attachments of the reply are resolved automatically. A response field that the endpoint sent as an xop:Include reads back as bytes, transparently, and the raw parts are also available as the reserved attachments attribute:

response = self.soap['Document Repository'].invoke('RetrieveDocumentSet', request)

for part in response.attachments:
    self.logger.info('Received %s (%s), %d bytes',
        part.content_id, part.content_type, len(part.data))

Each part exposes content_id, content_type and data.

SOAP with Attachments

The older SwA packaging - plain MIME multipart without XOP references - is what ebXML messaging uses for its payloads and is handled there. Regular SOAP connections use MTOM.

Learn more