5

Suppose I use python zeep to execute a query against a server like this:

from zeep import Client

wsdl_url = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL"

client = Client(wsdl_url)
result = client.service.ListOfContinentsByCode()

I would like to see the raw XML that comes back from this method, ie extract it somehow from result. Is this possible? How?

Marc
  • 3,386
  • 8
  • 44
  • 68

3 Answers3

8

I think what you are looking for is the history plugin.

from zeep.plugins import HistoryPlugin
from zeep import Client
from lxml import etree

wsdl_url = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL"

history = HistoryPlugin()
client = Client(wsdl_url, plugins=[history])
client.service.CurrencyName('GBP')
your_pretty_xml = etree.tostring(history.last_received["envelope"], encoding="unicode", pretty_print=True)

Trapli
  • 1,517
  • 2
  • 13
  • 19
  • 1
    if you get a zeep exception because the response does not validate, start the client with a `settings=Settings(strict=False)` – knipwim Sep 08 '20 at 06:25
3

If you are trying to debug the XML in a zeep request or response you can use standard Python logging. From the documentation:

To see the SOAP XML messages which are sent to the remote server and the response received you can set the Python logger level to DEBUG for the zeep.transports module.

logging.config.dictConfig({
    ...
    'loggers': {
        'zeep.transports': {
            'level': 'DEBUG',
            'propagate': True,
            'handlers': ['console'],
        },
    },
})
kjpc-tech
  • 531
  • 1
  • 5
  • 8
-3

Here (requests takes care of the gzip)

import requests
r = equests.get('http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL')
if r.status_code == 200:
    print(r.content)
balderman
  • 22,927
  • 7
  • 34
  • 52
  • Nope. This just gets the WSDL document. I want to get the XML response from a SOAP method that is described by the WSDL document. They are totally different things. – Marc Jul 25 '19 at 11:36
  • @Marc The url in your post points to the xsd which is a valid xml itself. So I thought you are interested in wsdl doc. Now I understand that you are interested in the xml data that zeep got from the wire and created the result for your call. Is that true? – balderman Jul 25 '19 at 11:40
  • @Marc Assuming you want to "see" the xml you can do the following (based on the fact the zeep is using requests as a Transport - https://github.com/mvantellingen/python-zeep/blob/4df383021e31372c111bc26cbf2e4535deaee04e/src/zeep/transports.py) , You can set the log level of requests to verbose so requests will print out the data he senst and get back. – balderman Jul 25 '19 at 11:51
  • @Marc If the above will not work. You will have to use requests hook.https://2.python-requests.org//en/master/user/advanced/#event-hooks - it will make requests call your code and from here you will be able to see the data. – balderman Jul 25 '19 at 11:57