2014-03-19 85 views
1

我正尝试使用其Core API在Zendesk中创建新的组织。我可以毫无问题地拨打其他电话,但这一次仍然失败。下面的代码演示了此问题:发生Python中的POST请求返回urllib2.HTTPError:HTTP错误400:错误的请求

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, '[email protected]', '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 

任何帮助将不胜感激!谢谢。

+0

使用以下帮助: http://stackoverflow.com/questions/4348061/how-to-使用的Python,urllib2的待送JSON的数据换登录 – user3173806

回答

0

您的问题与使用urllib.urlencode有关。作为the documentation表示该旨意:

Convert a mapping object or a sequence of two-element tuples to a “percent-encoded” string.

Zendesk Organizations API期待接收JSON数据作为请求的主体中。 Zendesk无法理解您的请求,因为它的格式不正确。返回的400状态码指示了这一点。在状态代码的Wikipedia Page描述了一个400个状态码:

The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

趁现在,你可以通过正确包括这样的请求中的数据解决这个问题:

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

你真的会为自己节省很多如果您使用了requests库,则需要付出努力。我没有访问的Zendesk来测试这一点,但我怀疑,因为你的代码可以改写:

import requests 

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