SOAP and MTOM/XOP in Python
python.org

SOAP and MTOM/XOP in Python

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:

N?o foi fornecido texto alternativo para esta imagem
Request body as basic SOAP Envelope

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.

  • boundary: indicates the beginning of each part, as well as the end of the message body.
  • MIME Headers: tell the server what type of content to expect next.

As for the Envelope, each occurrence of file data must be replaced with an <xop:Include> element.

  • <xop:Include>: works as a reference to another part of the message body that contains the actual file data. It does this through its href attribute, which must contain a unique CID.

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.

N?o foi fornecido texto alternativo para esta imagem
Request body as XOP Package

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        
N?o foi fornecido texto alternativo para esta imagem
Making a XOP Package with pymtom-xop and sending with Zeep

The process of uploading files with pymtom-xop is done in 4 steps:

  1. Create MtomAttachment for each file
  2. Create MtomTransport
  3. Add MtomAttachments to MtomTransport files list
  4. Insert CID of each MtomAttachment where its base64Binary data should be in the SOAP envelope

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:

  • https://www.ibm.com/docs/es/cics-ts/5.1?topic=data-mtomxop-soap
  • https://www.w3.org/TR/soap12-mtom/#mime-serialization

要查看或添加评论,请登录

社区洞察

其他会员也浏览了