SOAP attachments - MTOM and SwA

MTOM and XOP packaging from plain Python bytes, with response attachments as first-class objects.

Binary content - documents, images, scans - does not belong inline in XML. MTOM (Message Transmission Optimization Mechanism) moves binary values out of the envelope into MIME parts, replacing each with an xop:Include reference, which keeps messages small and is required by document-exchange profiles such as IHE document submission and retrieval.

In Zato it is one checkbox - MTOM on the SOAP tab of an outgoing connection - plus ordinary Python bytes.

Sending binary data

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.

Receiving binary data

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