SOAP and MTOM/XOP in Python
Gabriel Santos M.
Especialista SOC | Gest?o eSocial | Saúde e Seguran?a do Trabalho
MTOM/XOP is a method that can be used to optimize the transfer of file data through SOAP APIs.
It is commonly found in enterprise level applications built with Java and similar languages, where a large amount of data is expected to be received by the server on each request.
The basic idea behind the method is to separate the file data from the SOAP Envelope, thus making the message parts easier to process.
Basic SOAP vs MTOM/XOP
In a normal SOAP request, a file upload could be done by inserting the file data directly into the SOAP Envelope as base64Binary or hexBinary, as the example below:
The problem with this method is that the Envelope becomes huge because of the file data inside of it, harming the processing of the request.
With MTOM/XOP, the message body is split into multiple parts: Envelope + files.
Each part must be preceded by a boundary and its MIME Headers.
As for the Envelope, each occurrence of file data must be replaced with an <xop:Include> element.
It is also important to notice that in this case, the file data is not expected to be base64Binary or hexBinary anymore, but simple binary.
领英推荐
As you can see, this method is a lot more complicated than simply inserting the file inside the Envelope and sending it as a post request. Also a lot more prone to error because of the great amount of details the developer would have to keep track of.
pymtom-xop
While python does have many great libraries to assist its developers with SOAP APIs, nothing has been found yet to solve the problem of MTOM/XOP.
pymtom-xop is an attempt to fill that gap.
It was built on the shoulders of Zeep, a powerful library for SOAP integration in python3. And tries to simplify the creation of the XOP Package by using its own classes: MtomTransport and MtomAttachment.
How to use:
pip install pymtom-xop
The process of uploading files with pymtom-xop is done in 4 steps:
The library takes care of the request headers, boundaries, and MIME headers, before passing the message back to Zeep, who sends the request and processes the response.
Links:
References: