我試圖做使用下面的urllib2我計算器上找到的例子入土爲安:否則使用Python PUT的urllib2
Is there any way to do HTTP PUT in python
我不明白爲什麼我得到錯誤的錯誤。
這裏是我的代碼的摘錄:
import urllib2
import json
content_header = {'Content-type':'application/json',
'Accept':'application/vnd.error+json,application/json',
'Accept-Version':'1.0'}
baseURL = "http://some/put/url/"
f = open("somefile","r")
data = json.loads(f.read())
request = urllib2.Request(url=baseURL, data=json.dumps(jsonObj), headers=content_header)
request.get_method = lambda: 'PUT' #if I remove this line then the POST works fine.
response = urllib2.urlopen(request)
print response.read()
如果我刪除我試圖設置,將其發佈找到看跌期權,但它會出錯誤,當我嘗試設置GET_METHOD放。
爲了確保REST服務不會導致我嘗試使用cURL執行PUT並且它工作正常的問題。
你是什麼例外的全面回溯? –
如果你不想安裝一個新的Python庫,['python-requests'](http://docs.python-requests.org/en/latest/)可以處理'PUT'而不需要額外的入侵,更易於使用。 –
FWIW我強烈建議在Python中使用HTTP請求庫。內置HTTP客戶端功能的API不好。然而,這不等於答案,因爲你的問題指定了'urllib2'。 –