Python Zeep Request Example
If you are looking to send SOAP requests in Python, Zeep is a great option. It is a fast and lightweight SOAP client for Python, which makes it easy to consume web services. In this article, we will walk you through a simple example of how to make a SOAP request using Zeep in Python.
Firstly, you need to install Zeep using pip:
$ pip install zeep
Once you have installed Zeep, you can start making SOAP requests. Here is an example of how to create a client object and send a request:
from zeep import Client
client = Client('http://www.example.com/soap?wsdl')
result = client.service.get_data('parameter')
print(result)
In this example, we create a client object by passing the URL of the WSDL file to the Client constructor. We then call the get_data method of the web service by passing a parameter value. The result is stored in the result variable and printed to the console.
It is also possible to pass complex data structures as parameters to the SOAP request. Here is an example:
from zeep import Client
from zeep import xsd
client = Client('http://www.example.com/soap?wsdl')
complex_type = xsd.ComplexType([
xsd.Element('name', xsd.String()),
xsd.Element('age', xsd.Integer()),
])
data = {
'name': 'John Doe',
'age': 30,
}
result = client.service.get_data(complex_type(**data))
print(result)
In this example, we define a complex data structure using the xsd.ComplexType class. We then create a dictionary object containing the values for the name and age fields. We pass this dictionary object to the get_data method by converting it to a complex_type object using the ** operator.
Finally, it is worth noting that Zeep supports several authentication methods, including HTTP basic authentication and WS-Security. You can specify the authentication method when creating the client object. Here is an example:
from zeep import Client
from zeep.wsse.username import UsernameToken
client = Client('http://www.example.com/soap?wsdl', wsse=UsernameToken('username', 'password'))
result = client.service.get_data('parameter')
print(result)
In this example, we create a client object with HTTP basic authentication. We pass a UsernameToken object containing the username and password to the wsse parameter of the Client constructor.
We hope this article has given you a good introduction to making SOAP requests using Zeep in Python. With this powerful library, you can easily consume web services and automate your workflow.