Python Zeep is throwing unkown fault for soap request - python

I am new to using the framework zeep. I am trying to send a SOAP request . But I get the below incorrect data. I need to get the response in xml or csv format.
<Element {http://schemas.xmlsoap.org/soap/envelope/}Envelope at 0x7fabdc9ea888>
With the wsdl, I am able to fetch the correct output using SoapUI tool.
from requests import Session
from zeep import Client
from zeep.transports import Transport
from requests.auth import AuthBase, HTTPBasicAuth
import datetime
wsdl = 'http://XX.XXX.XX.XX:ZZZZ/TL/IM?wsdl'
session = Session()
session.auth = SymantecAuth('user','password', "http://XX.XXX.XX.XXX")
session.verify = False
transport = Transport(session=session)
client = Client(wsdl=wsdl, transport=transport)
request_data = {"platforms": "test", "platid": {"ID": "QI4552"}}
results=client.create_message(client.service, 'RetrieveID', request_data)
print(results)

Since you are creating the message, print(results) is just showing the message object created.
Instead this should work to print the message on screen:
from lxml import etree
# Your code follows here
results=client.create_message(client.service, 'RetrieveID', request_data)
# this will print the message to be sent to Soap service.
print(etree.tostring(results, pretty_print=True))
If you want to see the response of the RetrieveID operation. then do this instead (Provided this method is bound on 1st available binding):
response = client.service.RetrieveID(**request_data)
print(response)
Let us know if it doesn't work.

Related

I need to create a python script which calls SOAP GET method using ZEEP or REQUEST modules

I am new to python. I need to fetch data from Oracle fusion cloud. I want to run a BI publisher report on Oracle fusion cloud instance using SOAP API call and get the data into CSV file.
I have tried with python ZEEP and REQUESTS modules but I am not getting expected results.
For example:
My WSDL: https://xxx.yy.us6.oraclecloud.com/xmlpserver/services/ExternalReportWSSService?wsdl
The operation I need to use is from above WSDL is 'runReport'
When I am running this request from SOAP UI for 'runReport' operation I am getting expected result like below:
This screenshot is from SOAP UI where I am getting encoded data which is expected
I am using below code in python (Python 3.5) to call this API. I have used REQUESTS and ZEEP both:
1. REQUESTS Module:
from requests.auth import HTTPBasicAuth
from xml.etree import ElementTree
url="https://xxxx.yyyy.us6.oraclecloud.com/xmlpserver/services/ExternalReportWSSService?wsdl"
#headers = {'content-type': 'application/soap+xml'}
headers = {'content-type': 'text/xml'}
body = """<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:pub="http://xmlns.oracle.com/oxp/service/PublicReportService">
<soap:Header/>
<soap:Body>
<pub:runReport>
<pub:reportRequest>
<pub:attributeFormat>csv</pub:attributeFormat>
<!-- Flatten XML should always be false when we have XML type of output to display the XML tags as mentioned in BIP Data Model and display XML structure in as expected format -->
<pub:flattenXML>false</pub:flattenXML>
<pub:parameterNameValues>
<!--1st Parameter of BIP Report-->
<pub:item>
<pub:name>p_name</pub:name>
<pub:values>
<pub:item>tapan</pub:item>
</pub:values>
</pub:item>
<!--2nd Parameter of BIP Report-->
<!--<pub:item>
<pub:name>p_to_date</pub:name>
<pub:values>
<pub:item>10-15-2019</pub:item>
</pub:values>
</pub:item>-->
</pub:parameterNameValues>
<pub:reportAbsolutePath>/Custom/Integration/test_data_rpt.xdo</pub:reportAbsolutePath>
<!-- Setting sizeOfDataChunkDownload to -1 will return the output to the calling client -->
<pub:sizeOfDataChunkDownload>-1</pub:sizeOfDataChunkDownload>
</pub:reportRequest>
</pub:runReport>
</soap:Body>
</soap:Envelope>"""
response = requests.get(url,data=body,headers=headers,auth=HTTPBasicAuth('XXXX', 'XXXX'))
print (response.text)
Above code is just giving me the list of operations available in the WSDL
2. ZEEP Module
from zeep import Client
from requests import Session
from requests.auth import HTTPBasicAuth
from zeep.transports import Transport
wsdl = "https://XXXX.XXXX.us6.oraclecloud.com/xmlpserver/services/ExternalReportWSSService?wsdl"
session = Session()
session.auth = HTTPBasicAuth('XXXX', 'XXXX')
#An additional argument 'transport' is passed with the authentication details
client = Client(wsdl, transport=Transport(session=session))
request_payload= """<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:pub="http://xmlns.oracle.com/oxp/service/PublicReportService">
<soap:Header/>
<soap:Body>
<pub:runReport>
<pub:reportRequest>
<pub:attributeFormat>csv</pub:attributeFormat>
<!-- Flatten XML should always be false when we have XML type of output to display the XML tags as mentioned in BIP Data Model and display XML structure in as expected format -->
<pub:flattenXML>false</pub:flattenXML>
<pub:parameterNameValues>
<!--1st Parameter of BIP Report-->
<pub:item>
<pub:name>p_name</pub:name>
<pub:values>
<pub:item>tapan</pub:item>
</pub:values>
</pub:item>
<!--2nd Parameter of BIP Report-->
<!--<pub:item>
<pub:name>p_to_date</pub:name>
<pub:values>
<pub:item>10-15-2019</pub:item>
</pub:values>
</pub:item>-->
</pub:parameterNameValues>
<pub:reportAbsolutePath>/Custom/Integration/test_data_rpt.xdo</pub:reportAbsolutePath>
<!-- Setting sizeOfDataChunkDownload to -1 will return the output to the calling client -->
<pub:sizeOfDataChunkDownload>-1</pub:sizeOfDataChunkDownload>
</pub:reportRequest>
</pub:runReport>
</soap:Body>
</soap:Envelope>"""
response = client.service.runReport(request_payload)
#Here 'request_data' is the request parameter dictionary.
#Assuming that the operation named 'runReport' is defined in the passed wsdl.
Above code is not working because I am not sure how to pass the request payload using ZEEP module.
Please help me!!
I've used the requests module to dynamically schedule out reports from our Oracle Fusion cloud instance to UCM (slightly different than your request) but noticed the following differences in the content type distinction in the header and the method used for the response:
headers = {
"content-type" : "application/soap+xml"
}
response = requests.post(url, data=body, headers=headers)

Soap request works fine in Soapui but not in python - ZEEP - SUDS

I have a request to a soap webservice which is working fine in SoapUi. I'd like to call this webservice in a python script.
I tried some modules : zeep, suds,... but I always have an SSL error.
Here are the infos :
https://37.71XXXXXXACONYX?wsdl
Basic authentification : username + password
Authentification type : preemptive
I have the xml available in SoapUi.
Does someone have any ideas?
Thanks a lot !!
I tried :
from requests.auth import HTTPBasicAuth # or HTTPDigestAuth, or OAuth1, etc.
from requests import Session
from zeep import Client
from zeep.transports import Transport
session = Session()
session.auth = HTTPBasicAuth(user, password)
client = Client('https://37.71XXXXXXACONYX?wsdl',
transport=Transport(session=session))

SOAP API Get Cookie

I'm using a SOAP API to get an authentication key with a cookie that is suppose to be returned.
from zeep import Client
client = Client("AuthenticationService.xml")
result = client.service.ValidateUser(username, password, "")
result
However with the result, I am getting a Boolean of True but no Cookie that contains the authentication key.
From the below picture, you can see that the same request using SoapUI returns a cookie. I'm wondering how I can do this in Python.
To be able to handle cookie, we must use requests.Session for the transport.
So a simple use case would look like this for you:
from zeep import Client
from requests import Session
from zeep.transports import Transport
session = Session()
# disable TLS verification
session.verify = False
transport = Transport(session=session)
client = Client("AuthenticationService.xml", transport=transport)
result = client.service.ValidateUser(username, password, "")
# then check cookie
client.transport.session.cookies
hope this helps.

Python Soap client WSDL functions

I am trying to create a python soap client with zeep.
But i can't figure out how to use the functions which are defined in the WSDL.
Here is my code:
from requests import Session
from requests.auth import HTTPBasicAuth
import zeep
from zeep.transports import Transport
session = Session()
session.auth = HTTPBasicAuth('admin', 'ip411')
transport_with_basic_auth = Transport(session=session)
client =
zeep.Client(wsdl='http://10.8.20.27/pbx10_00.wsdl',transport=transport_with_basic_auth)
client.service.Initialize('soap','test',True,True,True,True,True)
You can look at the WSDL here: www.innovaphone.com/wsdl/pbx10_00.wsdl

When calling Rest API from Python 2.7 requests, it responds "reason" but I don't see that in my API

When I call the socialcast api from python 2.7 using requests, I get a response "reason" but I don't see that text in my actual API. Here's my code:
import requests
parameters = {"username" = "myUsername", "password" = "myPassword"}
response = requests.get("https://hub.sas.com/api/groups/808/messages.json", parameters)
response.json()
The beginning of the JSON that I'm passing through is this:
{"messages":[{"id":126433,"user":{"id":4468,"name":
So I would expect something else to come back, but what it returns is:
{u'reason': u''}
Is this an error or is there something I'm not understanding?
I solved my problem by using this code:
import requests
from requests.auth import HTTPBasicAuth
r = requests.get('https://hub.sas.com/api/groups/808/messages.json', auth=HTTPBasicAuth('username', 'password'))
data = r.json()
for message in data['messages']:
print(message['user']['name'])
I'm not sure that the from requests.auth import HTTPBasicAuth or the data = r.json() were necessary but it ended up working for me so I left them in there.

Categories

Resources