简体   繁体   English

urllib2.HTTPError:HTTP错误400:错误的请求-Python

[英]urllib2.HTTPError: HTTP Error 400: Bad Request - Python

I'm trying to POST using urllib and urllib2 but it keeps giving me this error 我正在尝试使用urllib和urllib2进行开机自检,但它一直给我这个错误

    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

and here's my code 这是我的代码

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()

Anyone could help me here ? 有人可以帮我吗?

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作为json字符串发送,而不是将字典转换为字符串。

body = '{"where" : {"deviceType" : "ios"}, "data" : {"alert" : "vvv"}}'

Notice the use of double-quotes for the json elements. 请注意,对json元素使用了双引号。

I suspect your payload is not correctly encoded. 我怀疑您的有效负载编码不正确。 Nowhere they say "urlencoded". 他们无处说“ urlencoded”。 Try using their own example instead. 尝试使用自己的示例代替。 https://parse.com/docs/rest#push urrlib2 is a rather primitive library - using eg requests would be more convenient. https://parse.com/docs/rest#push urrlib2是一个相当原始的库-使用例如请求会更方便。

It depends whether the json data is in correct format or not, or the issue is in the header content. 这取决于json数据的格式是否正确,还是标题内容存在问题。 Hence the urllib2.HTTPError: HTTP Error 400: Bad Request usually occurs. 因此, urllib2.HTTPError: HTTP Error 400: Bad Request通常会发生。

A small piece of code below with the basic64 authentication with the headers and the json data for the PUT/POST methods: 下面是一小段带有basic64身份验证的代码,其中包含PUT / POST方法的标头和json数据:

import urllib2
import base64
import json
url = 'Your_URL'
auth = 'Basic ' + base64.encodestring('%s:%s' % ('username','password'))[:-1]
content_header = {'Authorization': auth,
                 'Content-Type':'application/json',
                 'Accept':'application/json'}
json_data = YOUR_DATA
request = urllib2.Request(url=url, data=json.dumps(json_data), headers=content_header)
request.get_method = lambda: 'PUT' #If you will not provide this, then it will be POST
response = urllib2.urlopen(request)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM