python using urllib2 and urllib get request issue



当我使用 curl 获取请求成功时,但使用 python 调用获取请求失败,任何人都可以提供帮助!

shell script:
curl -X GET -H 'Content-type: application/json' -H 'Authorization: Bearer '$token'' -d '[ { "id":"535985", "language":"EN", "Detailes":{"LibraryId":"KF_2gl9xZYKX7TJi66" }, "KeyID":"SF_cY1tKhYiocNluBB" } ]' 'https://XXXX.com'

获取请求成功。

python Script:
data ={ "id":"535985", "language":"EN", "Detailes":{"LibraryId":"KF_2gl9xZYKX7TJi66" }, "KeyID":"SF_cY1tKhYiocNluBB" }
headers = {'content-type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Bearer %s' % (token)}
proxy = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
DataForGet=urllib.urlencode(data)
NewUrl= apilink + "?" + DataForGet
request = urllib2.Request(NewUrl, headers)
response = urllib2.urlopen(request, timeout=300)
message = response.read()

返回消息 类型错误:不可哈希类型

任何人都知道如何通过 urllib2 和 urllib 模型使用 get request。非常感谢!

我发现我们可以定义这个mothod来调用api获取数据

class RequestMethod(urllib2.Request,object):
def __init__(self, url, method=None,data=None, headers={},origin_req_host=None, unverifiable=False):
self.method = method
super(RequestMethod,self).__init__(url, data=data,headers=headers,origin_req_host=origin_req_host,unverifiable=unverifiable)
def get_method(self):
if self.method != None:
return self.method
else:
return super(RequestMethod,self).get_method()
def set_method(self,method):
self.method = method
def setMethodRequest(url,method):
return RequestMethod(url,method=method)

最新更新