2016-06-28 49 views
0

我必须在Python中使用使用urllib.request的POST方法,并为POST方法编写了以下代码。我们如何在Python中使用POST方法使用urllib.request?

values = {"abcd":"efgh"} 
headers = {"Content-Type": "application/json", "Authorization": "Basic"+str(authKey)} 
req = urllib.request.Request(url,values,headers=headers,method='POST') 
response = urllib.request.urlopen(req) 
print(response.read()) 

我能够利用“GET”“删除”但不“POST” .Could任何人帮我解决这个? 谢谢

+0

在问题中添加'url',它是什么? – bhansa

回答

1

您可以使用请求模块。

import requests 
... 
url="https://example.com/" 
print url 
data = {'id':"1", 'value': 1} 
r = requests.post(url, data=data) 
print(r.text) 
print(r.status_code, r.reason) 
+1

无需请求和urllib.request,有没有办法? –