# How to print Zeep raw requests and responses

I've somehow found myself in the situation of consuming a SOAP. We're using the Python Zeep (opens new window) package as a SOAP client. Zeep has been super useful so far to me as someone who doesn't have a background in SOAP APIs.

Today I learned how to log the raw XML requests that you've sent through Zeep, as well as the responses to those requests.

This Github issue (opens new window) show me how to do it. In particular, this comment (opens new window).

Zeep has a history plugin (opens new window). You don't have to install anything extra to start using it.

# Steps

  1. Instantiate a History object using:
from zeep.plugins import HistoryPlugin

history = HistoryPlugin()
1
2
3
  1. Pass it to your Zeep Client:
from zeep import Client

client = Client(
    'http://examples.python-zeep.org/basic.wsdl',
    plugins=[history])
1
2
3
4
5
  1. After making some requests through your Client, you can print your requests and responses with:
from lxml import etree

for hist in [history.last_sent, history.last_received]:
    print(etree.tostring(hist["envelope"], encoding="unicode", pretty_print=True))
1
2
3
4

Newsletter

If you'd like to subscribe to my blog, please enter your details below. You can unsubscribe at any time.

Powered by Buttondown.

Last Updated: 12/20/2024, 11:25:10 AM