gpt4 book ai didi

python - 类型错误 : can't concat bytes to str python3

转载 作者:行者123 更新时间:2023-11-30 22:37:24 25 4
gpt4 key购买 nike

我正在尝试使用 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

关于python - 类型错误 : can't concat bytes to str python3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43901079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com