tugrulv
tugrulv

Reputation: 31

Python Zeep WSDL Unexpected Elements Traceback

I can't handle this below.

XMLParseError: Unexpected element 'metadata', expected 'id' error.

I also try strict=False settings but that time zeep returned as none.

from zeep import Client
import zeep
wsdl = 'https://api.n11.com/ws/CategoryService.wsdl'

cl = Client(wsdl=wsdl)
request_data = {"auth":{'appKey' : ***,
'appSecret' : **},'categoryId':int(1003221),"pagingData":{'pageSize':1,'currentPage':0}}
print(cl.service.GetCategoryAttributes(**request_data))

Thanks.

Upvotes: 2

Views: 2712

Answers (2)

Mehmet Gülsoy
Mehmet Gülsoy

Reputation: 41

I faced the same problem with the same API. My solution is changing zeep's settings. You should use xsd_ignore_sequence_order=True. I hope it's not too late.

settings = Settings(strict=False, xml_huge_tree=True, xsd_ignore_sequence_order=True)

read more: https://docs.python-zeep.org/en/master/settings.html#module-zeep.settings

Upvotes: 3

tugrulv
tugrulv

Reputation: 31

Solution:

firstly, to check logs:

import logging.config
logging.config.dictConfig({
    'version': 1,
    'formatters': {
        'verbose': {
            'format': '%(name)s: %(message)s'
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'zeep.transports': {
            'level': 'DEBUG',
            'propagate': True,
            'handlers': ['console'],
        },
    }
})

Second:

from zeep import Client
from zeep.plugins import HistoryPlugin
wsdl = 'https://api.n11.com/ws/CategoryService.wsdl'
from zeep import Client, Settings
settings = Settings(strict=False, xml_huge_tree=True)
history = HistoryPlugin()
from zeep.transports import Transport
transport = Transport(timeout=10)
key={'appKey' : '**',
'appSecret' : '**',
}
cat= {'categoryId':1003221}
dat= {'pageSize':1,'currentPage':1}
client = Client(wsdl=wsdl, transport=transport, plugins=[history], settings=settings)
with client.settings(raw_response=False,strict=False, xml_huge_tree=True):
    response = client.service.GetCategoryAttributes(key,1003221,pagingData=dat)
    #assert response.status_code == 200
    #assert response.content
#node = client.create_message(client.service, 'GetCategoryAttributes',key,1003221,pagingData=dat)
from lxml import etree

try:
    for hist in [history.last_sent, history.last_received]:
        print(etree.tostring(hist["envelope"], encoding="unicode", pretty_print=True))   
except (IndexError, TypeError):
    # catch cases where it fails before being put on the wire
    pass

lastly, you can parse with bs4 for xml content:

from bs4 import BeautifulSoup
soup = BeautifulSoup(ass, 'lxml')
soup.find('category').find('metadata')

Upvotes: 1

Related Questions