urllib2.HTTPError:HTTP 错误 400:错误请求 - Python [英] urllib2.HTTPError: HTTP Error 400: Bad Request - Python
本文介绍了urllib2.HTTPError:HTTP 错误 400:错误请求 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 urllib 和 urllib2 进行 POST,但它一直给我这个错误
I'm trying to POST using urllib and urllib2 but it keeps giving me this error
Traceback (most recent call last):
File "/Users/BaDRaN/Desktop/untitled text.py", line 39, in <module>
response = urllib2.urlopen(request)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
这是我的代码
body = {'where' : {'deviceType' : 'ios'}, 'data' : {'alert' : 'vvv'}}
headers = { 'X-Parse-Application-Id' : 'someID', 'X-Parse-REST-API-Key' : 'someKey', 'Content-Type' : 'application/json' }
data = urllib.urlencode(body)
request = urllib2.Request('https://api.parse.com/1/push', data, headers)
response = urllib2.urlopen(request)
call = response.read()
有人可以帮我吗?
推荐答案
我的超能力建议您希望将 body
作为 json 字符串发送,而不是作为转换为字符串的字典发送.
My psychic powers suggest you want to send body
as a string of json, not as a dictionary that's converted to a string.
body = '{"where" : {"deviceType" : "ios"}, "data" : {"alert" : "vvv"}}'
注意对 json 元素使用双引号.
Notice the use of double-quotes for the json elements.
这篇关于urllib2.HTTPError:HTTP 错误 400:错误请求 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文