# SOAP attachments - MTOM and SwA

MTOM and XOP packaging from plain Python bytes, with response attachments returned as Python 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 {#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:

```python
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 {#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:

```python
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 {#soap-with-attachments}

The older SwA packaging - plain MIME multipart without XOP references - is what [ebXML messaging](https://zato.io/docs/dev/soap/ebxml/index.html) uses for its payloads and is handled there. Regular SOAP connections use MTOM.

## Learn more {#learn-more}

- [SOAP integrations](https://zato.io/docs/dev/soap/) - Envelopes, versions and Python objects instead of XML
- [SOAP as Python objects](https://zato.io/docs/dev/soap/python-objects/) - Build requests and read responses with dot access, no schemas
- [SOAP security](https://zato.io/docs/dev/soap/security/) - WS-Security, credentials in the body and client certificates
- [SOAP mandates](https://zato.io/docs/dev/soap/mandates/) - What each national and sector profile requires, and how to meet it
- [SOAP tutorial](https://zato.io/tutorials/soap/calling.html) - Call any SOAP service with Python objects, start to finish
