python - 如何在 Python 中使用 zeep 设置默认 xmlns?
问题描述
我想使用 zeep 进行一些 API 调用。预期的输出是:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="https://cabapi-prod.service.dbrent.net/hal2_cabserver/">
<soap:Body>
<CABSERVER.listFreeBikes>
<CommonParams>
<UserData>
<User>XXXXXXXXXXXXXX</User>
<Password>XXXXXXXXXXXXXX</Password>
</UserData>
<LanguageUID>1</LanguageUID>
<RequestTime>XXXXXXXXXXXXXX</RequestTime>
<Version>2</Version>
</CommonParams>
<SearchPosition>
<Longitude>XXXXXXXXXXXXXX</Longitude>
<Latitude>XXXXXXXXXXXXXX</Latitude>
</SearchPosition>
<maxResults>100</maxResults>
<searchRadius>844</searchRadius>
<CustomerData>
<Phone>XXXXXXXXXXXXXX</Phone>
<Password>XXXXXXXXXXXXXX</Password>
<PhoneUserEdited>false</PhoneUserEdited>
</CustomerData>
</CABSERVER.listFreeBikes>
</soap:Body>
</soap:Envelope>
但是,我只能得到:
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Body>
<ns0:CABSERVER.listFreeBikes xmlns:ns0="https://xml.dbcarsharing-buchung.de/hal2_cabserver/">
<CommonParams>
<UserData>
<User>XXXXXXXXXXXXXX</User>
<Password>XXXXXXXXXXXXXX</Password>
</UserData>
<LanguageUID>1</LanguageUID>
<RequestTime>XXXXXXXXXXXXXX</RequestTime>
<Version>2</Version>
</CommonParams>
<CustomerData>
<Phone>XXXXXXXXXXXXXX</Phone>
<Password>XXXXXXXXXXXXXX</Password>
<PhoneUserEdited>false</PhoneUserEdited>
</CustomerData>
<SearchPosition>
<Longitude>XXXXXXXXXXXXXX</Longitude>
<Latitude>XXXXXXXXXXXXXX</Latitude>
</SearchPosition>
<maxResults>100</maxResults>
<searchRadius>844</searchRadius>
</ns0:CABSERVER.listFreeBikes>
</soap-env:Body>
</soap-env:Envelope>
服务器无法正确识别我的请求,我认为这是因为命名空间。我做了一些研究,但没有找到将默认命名空间 (xmlns) 设置为https://xml.dbcarsharing-buchung.de/hal2_cabserver/的方法。相反,它被添加为额外的命名空间。
这是我当前的代码:
with client.settings(raw_response=True):
## Common Params related
### UserData related
userNameType = client.get_type('ns0:Type_UserName')
userPasswordType = client.get_type('ns0:Type_UserPassword')
userName = userNameType('XXXXXXXXXXXXXX')
userPassword = userPasswordType('XXXXXXXXXXXXXX')
userDataType = client.get_type('ns0:Type_UserData')
userData = userDataType(User = userName, Password = userPassword)
current_time = time.localtime()
current_time_str = time.strftime('%Y-%m-%dT%H:%M:%S.GMT+02:00', current_time)
commonParamsType = client.get_type('ns0:Type_CommonParams')
commonParams = commonParamsType(UserData = userData, LanguageUID = 1, RequestTime = current_time_str, Version = 2)
## CustomerData related
customerPhoneType = client.get_type('ns0:Type_CustomerPhone')
customerPhone = customerPhoneType('XXXXXXXXXXXXXX') # Get from config
customerPasswordType = client.get_type('ns0:Type_CustomerPassword')
customerPassword = customerPasswordType('XXXXXXXXXXXXXX') #Get from config
customerDataType = client.get_type('ns0:Type_CustomerData')
customerData = customerDataType(Phone = customerPhone, Password = customerPassword, PhoneUserEdited = False)
## SearchPosition related
geoPositionType = client.get_type('ns0:Type_GeoPosition')
geoPosition = geoPositionType(Longitude = XXXXXXXXXXXXXX, Latitude = XXXXXXXXXXXXXX)
# DEBUG
node = client.create_message(client.service, 'CABSERVER.listFreeBikes', CommonParams = commonParams, CustomerData = customerData, SearchPosition = geoPosition, maxResults = 100, searchRadius = 844)
from lxml import etree as ET
tree = ET.ElementTree(node)
tree.write('test.xml', pretty_print=True)
到目前为止我尝试了什么:
client.set_ns_prefix('ns0', '')
但是,这并不能解决我的问题。我的想法不多了。谁能指出我正确的方向?
可以在此处找到 WSDL:https ://xml.dbcarsharing-buchung.de/hal2_cabserver/definitions/HAL2_CABSERVER_3.wsdl
解决方案
你很亲近
client.set_ns_prefix('ns0', '')
应该读
client.set_ns_prefix(None, "https://xml.dbcarsharing-buchung.de/hal2_cabserver/")
然后应该使用默认命名空间。
推荐阅读
- html - Azure AD B2C 自定义策略社交按钮顺序
- node.js - socket.io v.4 加入时如何获取已在房间中的套接字列表
- php - 在特定页面中加载自定义主题/在 wordpress 插件 (PHP) 中发布
- java - 嵌套异常是 io.lettuce.core.RedisException: java.io.IOException: An现有连接被远程主机强行关闭
- c# - 当用户没有自己的邮箱时,我们如何通过 EWS 访问交换邮箱?
- flutter - Flutter:如何仅使用主屏幕中的按钮导航到第二个导航屏幕(底部导航)?
- javascript - 如何检查所有网络调用是否都在 React 中完成?
- symbols - 解析已加载的 ELF 文件
- python - 导出带注释的 GATE 文件以在 Python 中进行进一步处理会导致字符偏移问题
- django - 有没有办法为用户名字段添加样式 - django 表单