Goomba Stomp Goomba Stomp - 1 year ago 194
Python Question

TypeError: unhashable type in python script using urllib

I'm attempting to execute the following curl command in a python script using urllib:

curl -H "token:<COMPANY_TOKEN>" -X PUT \

Here's what I have in Python:

import urllib
import urllib2

theToken = <TOKEN>
headers = {"token":theToken}

checkOutUrl = "https://<DOMAIN><ASSET_ID>/checkout.api?user_id=<USER_ID>"

req = urllib2.Request(checkOutUrl, headers)
response = urllib2.urlopen(req)
thePage =

I understand this particular error will come up without the following:

data = urllib.urlencode(values)

but this command has no data and works fine from a shell prompt.

Answer Source

You are trying to pass off the headers as the request body, the data argument. Don't pass those headers in as a positional argument, use a keyword argument instead:

req = urllib2.Request(checkOutUrl, headers=headers)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download