eterry28
eterry28

Reputation: 59

python unhashable type - posting xml data

First, I'm not a python programmer. I'm an old C dog that's learned new Java and PHP tricks, but python looks like a pretty cool language.

I'm getting an error that I can't quite follow. The error follows the code below.

import httplib, urllib
url = "pdb-services-beta.nipr.com"
xml = '<?xml version="1.0"?><!DOCTYPE SCB_Request SYSTEM "http://www.nipr.com/html/SCB_XML_Request.dtd"><SCB_Request Request_Type="Create_Report"><SCB_Login_Data CustomerID="someuser" Passwd="somepass" /><SCB_Create_Report_Request Title=""><Producer_List><NIPR_Num_List_XML><NIPR_Num NIPR_Num="8980608" /><NIPR_Num NIPR_Num="7597855" /><NIPR_Num NIPR_Num="10166016" /></NIPR_Num_List_XML></Producer_List></SCB_Create_Report_Request></SCB_Request>'

params = {}
params['xmldata'] = xml

headers = {}
headers['Content-type'] = 'text/xml'
headers['Accept'] = '*/*'
headers['Content-Length'] = "%d" % len(xml)

connection = httplib.HTTPSConnection(url)
connection.set_debuglevel(1)
connection.request("POST", "/pdb-xml-reports/scb_xmlclient.cgi", params, headers)

response = connection.getresponse()
print response.status, response.reason
data = response.read()
print data
connection.close

Here's the error:

Traceback (most recent call last):   
 File "C:\Python27\tutorial.py", line 14, in  connection.request("POST", "/pdb-xml-reports/scb_xmlclient.cgi", params, headers)   
 File "C:\Python27\lib\httplib.py", line 958, in request self._send_request(method, url, body, headers)   
 File "C:\Python27\lib\httplib.py", line 992, in _send_request self.endheaders(body)
 File "C:\Python27\lib\httplib.py", line 954, in endheaders self._send_output(message_body)
 File "C:\Python27\lib\httplib.py", line 818, in _send_output self.send(message_body)
 File "C:\Python27\lib\httplib.py", line 790, in send self.sock.sendall(data)
 File "C:\Python27\lib\ssl.py", line 229, in sendall v = self.send(data[count:]) 
TypeError: unhashable type

My log file says that the xmldata parameter is empty.

Any ideas?

Upvotes: 2

Views: 1906

Answers (2)

eterry28
eterry28

Reputation: 59

Thanks for the feedback.

I guess I was making this too hard. I went a different route and it seems to work.

import urllib2
URL = "https://pdb-services-beta.nipr.com/pdb-xml-reports/scb_xmlclient.cgi"
DATA = 'xmldata=<?xml version="1.0"?><!DOCTYPE SCB_Request SYSTEM "http://www.nipr.com/html/SCB_XML_Request.dtd"><SCB_Request Request_Type="Create_Report"><SCB_Login_Data CustomerID="someuser" Passwd="somepass" /><SCB_Create_Report_Request Title=""><Producer_List><NIPR_Num_List_XML><NIPR_Num NIPR_Num="8980608" /></NIPR_Num_List_XML></Producer_List></SCB_Create_Report_Request></SCB_Request>'
req = urllib2.Request(url=URL, data=DATA)
f = urllib2.urlopen(req)
print f.read()

Upvotes: 0

Christoph Eberhardt
Christoph Eberhardt

Reputation: 450

I guess params has to be a string when passing to .request, this would explain the error, due to the fact, that a hash is not hashable

Try to encode your params first with

params = urllib.urlencode(params)

You can find another code example too at the bottom of:

http://docs.python.org/release/3.1.5/library/http.client.html

Upvotes: 2

Related Questions