Using urllib2 to do SOAP POST, but I keep getting error

I try to make an API call through SOAP POST, and I keep getting "TypeError: not a valid sequence, not associated with a string, or a matching object". @data = urllib.urlencode (values)

SM_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Header> <AutotaskIntegrations xmlns="http://Autotask.net/ATWS/v1_5/"> <PartnerID>partner id</PartnerID> </AutotaskIntegrations> </soap:Header> <soap:Body> <getThresholdAndUsageInfo xmlns="http://Autotask.net/ATWS/v1_5/"> </getThresholdAndUsageInfo> </soap:Body> </soap:Envelope>""" values = SM_TEMPLATE%() data = urllib.urlencode(values) req = urllib2.Request(site, data) response = urllib2.urlopen(req) the_page = response.read() 

Any help would be greatly appreciated.

+4
source share
2 answers

The urllib.urlencode function expects a sequence of key-value pairs or a display type, for example, dict :

 >>> urllib.urlencode([('a','1'), ('b','2'), ('b', '3')]) 'a=1&b=2&b=3' 

To perform an HTTP POST POST, you must leave the SM_TEMPLATE BLOB template as set and set it as the POST body, and then add a Content-Type header to encode and encode the POST body. For instance:

 data = SM_TEMPLATE headers = { 'Content-Type': 'application/soap+xml; charset=utf-8' } req = urllib2.Request(site, data, headers) 
+5
source

See the code below as an example, it can help you solve your SOAP request using urllib2 for Python 2.6.6. This helped me invoke Oracle Data Integrator (Oracle ODI). Obviously, you SHOULD adapt the values ​​for those that fit your case:

 import urllib2 url = "http://alexmoleiro.com:20910/oraclediagent/OdiInvoke?wsdl" post_data = """<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <Body> what_you_want_to_send_in_a_correct_format </Body> </Envelope> """ http_headers = { "Accept": "application/soap+xml,multipart/related,text/*", "Cache-Control": "no-cache", "Pragma": "no-cache", "Content-Type": "text/xml; charset=utf-8" } request_object = urllib2.Request(url, post_data, http_headers) #DELETE THIS BLOCK IF YOU ARE NOT USING PROXIES http_proxy_server = "10.1.2.3" http_proxy_port = "8080" http_proxy_realm = http_proxy_server http_proxy_full_auth_string = "http://%s:%s" % (http_proxy_server, http_proxy_port) proxy = urllib2.ProxyHandler({'http': http_proxy_full_auth_string}) opener = urllib2.build_opener(proxy) urllib2.install_opener(opener) #END OF --> DELETE THIS BLOCK IF YOU ARE NOT USING PROXIES response = urllib2.urlopen(request_object) html_string = response.read() print html_string 

Any feedback would be appreciated :-)

0
source

Source: https://habr.com/ru/post/1342459/


All Articles