TypeError: can't concat bytes to str python3

TypeError: can't concat bytes to str python3

我正在尝试使用 urllib3python3 中发送 HTTP 请求。

这是代码片段

request_body = {'grant_type':'password','username': username,'password': password}
request_headers = {'Content-Type' : 'application/x-www-form-urlencoded','Authorization': "hash string"}
http = urllib3.PoolManager()
response = http.request('POST', 'https://api/url/endpoint', headers=request_headers, body=request_body)

但是当我尝试执行它时,它会抛出以下错误。

TypeError: can't concat bytes to str

完整追溯

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Mubin/anaconda/lib/python3.6/site-
packages/urllib3/request.py", line 70, in request
**urlopen_kw)
File "/Users/Mubin/anaconda/lib/python3.6/site-
packages/urllib3/request.py", line 148, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
File "/Users/Mubin/anaconda/lib/python3.6/site-
  packages/urllib3/poolmanager.py", line 321, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/Users/Mubin/anaconda/lib/python3.6/site-
  packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/Users/Mubin/anaconda/lib/python3.6/site-
  packages/urllib3/connectionpool.py", line 356, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/Users/Mubin/anaconda/lib/python3.6/http/client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/Users/Mubin/anaconda/lib/python3.6/http/client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/Users/Mubin/anaconda/lib/python3.6/http/client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/Users/Mubin/anaconda/lib/python3.6/http/client.py", line 1064, in _send_output
+ b'\r\n'

谁能指出我做错了什么?

编辑

request_body 类型检查

>>> type(request_body)
    <class 'dict'>
>>> type(request_body['username'])
    <class 'str'>
>>> type(request_body['password'])
    <class 'str'>
>>> type(request_body['grant_type'])
    <class 'str'>

我认为您打算使用 fields 参数而不是 body

http.request('POST', 'https://api/url/endpoint', headers=request_headers, fields=request_body)

喜欢这个example

我找到了解决这个问题的方法。 我试图从 keycloak 获取令牌。 Keycloak 除外:
POST请求
内容类型:application/x-www-form-urlencoded
正文中有凭据

我在 https://urllib3.readthedocs.io/en/latest/reference/index.html#module-urllib3.request 中读到:

request_encode_body(method, url, fields=None, headers=None, encode_multipart=True, multipart_boundary=None, **urlopen_kw)

Make a request using urlopen() with the fields encoded in the body. This is useful for request methods like POST, PUT, PATCH, etc.

When encode_multipart=True (default), then urllib3.filepost.encode_multipart_formdata() is used to encode the payload with the appropriate content type. Otherwise urllib.urlencode() is used with the ‘application/x-www-form-urlencoded’ content type.

所以我成功完成了这样的请求:

    data = {
        'client_id': 'front',
        'grant_type': 'password',
        'username': settings.AUTH_REST_ADMIN_API_USER,
        'password': settings.AUTH_REST_ADMIN_API_PASSWORD
    }

    r = http.request_encode_body(
        'POST',
        settings.token_endpoint,
        encode_multipart=False,
        headers={
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': settings.AUTH_AUTHORIZATION
        },
        fields=data
    )

request_encode_body 方法的 encode_multipart=False 部分解决了我的问题。