# How to print Zeep raw requests and responses
Thu Feb 17 2022
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
- Instantiate a
History
object using:
from zeep.plugins import HistoryPlugin
history = HistoryPlugin()
1
2
3
2
3
- 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
2
3
4
5
- After making some requests through your
Client
, you canprint
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
2
3
4
Newsletter
If you'd like to subscribe to my blog, please enter your details below. You can unsubscribe at any time.