urllib.error.HTTPError: Ошибка HTTP 400: неверный запрос для слабой зацепки Python3 - PullRequest
0 голосов
/ 11 ноября 2019

Использование Python3 в командной строке repl:

Что я не так делаю в формате моего запроса?

Я получаю

urllib.error.HTTPError: HTTP Error 400: Bad Request

, когда делаю

>>>> from urllib import request
>>>> import urllib.parse

>>> data = urllib.parse.urlencode({"channel": "#aws_lambda_python_int", "username": "webhookbot", "text": "This is posted to #aws_lambda_python_int and comes from a bot named webhookbot.", "icon_emoji": ":ghost:"})                                                                                   
>>> data_encoded = data.encode('ascii')
>>> with urllib.request.urlopen("https://hooks.slack.com/services/THGF2356K/BQC7FJZ52/d1Ary9idSP5AAAAAAAAAAAA", data_encoded) as f:                
...     print(f.read().decode('utf-8'))
...

Я получаю

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/usr/lib/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.7/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.7/urllib/request.py", line 649, in http_error_default                                                                      
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
>>>
...