Goomba Stomp Goomba Stomp - 1 month ago 11
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 \
https://<DOMAIN>.ezofficeinventory.com/assets/<ASSET_ID>/checkout.api?user_id=<USER_ID>


Here's what I have in Python:

import urllib
import urllib2

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

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

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


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

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)