使用Zeep在Python中发起SOAP请求时遇ValidationError求助
Hey there, let's break down why you're hitting this error and how to fix it!
What's causing the error?
The ValidationError is telling you that the SOAP request generated by Zeep is missing a required field OPRID under the root element. Here's the core reason:
- Zeep strictly adheres to the rules defined in your target WSDL file. If the WSDL specifies that
OPRIDis a mandatory field for theget_methodoperation, Zeep will validate your input and throw an error if it's missing. - Your sample XML might be outdated, or the service provider may have updated their WSDL to require
OPRIDeven if older versions didn't enforce it.
How to fix this?
Let's walk through actionable solutions:
1. Inspect the WSDL's required fields
First, confirm exactly what fields the service expects. Use Zeep's built-in tools to check the WSDL structure:
from zeep import Client from zeep.wsse.username import UsernameToken wsdl = 'your_wsdl_url_here' client = Client(wsdl, wsse=UsernameToken('USERNAME', '1234')) # Print the method signature to see required parameters print(client.service.get_method) # Or dump the full WSDL structure for deeper inspection client.wsdl.dump()
Running this will show you all mandatory fields for get_method—you'll see OPRID marked as a required entry.
2. Add the missing OPRID parameter
Once you have a valid value for OPRID (check with the service provider if you're unsure), update your code to include it:
wsdl = 'your_wsdl_url_here' client = Client(wsdl, wsse=UsernameToken('USERNAME', '1234')) # Include the mandatory OPRID field alongside EMAIL response = client.service.get_method(OPRID='your_valid_oprid_value', EMAIL='david')
3. Disable strict validation (last resort)
If you're certain the service doesn't actually require OPRID (e.g., the WSDL has an error), you can turn off Zeep's strict validation. Note this is a workaround, not a best practice:
wsdl = 'your_wsdl_url_here' # Add strict=False to skip Zeep's input validation client = Client(wsdl, wsse=UsernameToken('USERNAME', '1234'), strict=False) response = client.service.get_method(EMAIL='david')
A quick note about your sample XML
Your manual XML example doesn't include OPRID, but that's likely why it would fail too (unless the service is lenient with manual requests but enforces rules for SOAP clients). Zeep's strict validation is actually helping you catch issues that might lead to silent failures later!
内容的提问来源于stack exchange,提问作者caratpine

