簡體   English   中英

Python中的POST請求返回urllib2.HTTPError:HTTP錯誤400:錯誤的請求

[英]POST request in Python returns urllib2.HTTPError: HTTP Error 400: Bad Request

我正在嘗試使用其Core API在Zendesk中創建新的組織。 我已經能夠打出其他電話而沒有問題,但是這個電話仍然失敗。 以下代碼演示了該問題:

url = "https://mydomain.zendesk.com/api/v2/organizations.json"
new_org = {"organization": {"name": "new organization"}}
data = urllib.urlencode(new_org)
req = urllib2.Request(url,data)
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, url, 'me@email.com', 'fakepassword')
auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_manager)
urllib2.install_opener(opener)
response = urllib2.urlopen(req)
bla = response.read()

發生以下錯誤:

  Traceback (most recent call last):
  File "/network/nfshome0/homestore00/me/workspace/Pythony/pythony/test2.py", line   35, in <module>
    response = urllib2.urlopen(req)
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 406, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 438, in error
    result = self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 890, in http_error_401
    url, req, headers)
  File "/usr/lib/python2.7/urllib2.py", line 865, in http_error_auth_reqed
    response = self.retry_http_basic_auth(host, req, realm)
  File "/usr/lib/python2.7/urllib2.py", line 878, in retry_http_basic_auth
    return self.parent.open(req, timeout=req.timeout)
  File "/usr/lib/python2.7/urllib2.py", line 406, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 444, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request

任何幫助將不勝感激! 謝謝。

您的問題與urllib.urlencode的使用有關。 文檔所示,這將:

將映射對象或兩個元素的元組序列轉換為“百分比編碼”字符串。

Zendesk Organizations API希望接收JSON數據作為請求的主體。 Zendesk無法理解您的請求,因為其格式錯誤。 返回的400狀態代碼表明了這一點。 關於狀態碼的Wikipedia頁面將400狀態碼描述為:

由於某些東西被認為是客戶端錯誤(例如, 格式錯誤的請求語法 ,無效的請求消息框架或欺騙性的請求路由),服務器無法或將不會處理請求。

現在,您可以通過在請求中正確包含數據來解決此問題,如下所示:

req = urllib2.Request(url, json.dumps(new_org))

如果使用了請求庫,您將真的省下了很多精力。 我沒有訪問Zendesk進行測試的權限,但是我懷疑您的代碼可以重寫為:

import requests

new_org = {"organization": {"name": "new organization"}}
response = requests.post(
    "https://mydomain.zendesk.com/api/v2/organizations.json",
    auth=('me@email.com', 'fakepassword'),
    data=new_org
)
data = response.json()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM