我正在尝试提出授权请求并提出TypeError: can't concat str to bytes
. 我在网上搜索并发现了类似的问题,但没有一个可以帮助解决我的问题。我目前在 Jupyter 工作(如果这有帮助?)我也是一个 python 菜鸟,所以请温柔点......
这是我的代码:
import http.client as httplib
import urllib.request, urllib.parse, urllib.error, base64
import requests as rq
base_url = 'api/url'
end_point = '/endpoint'
full_url = base_url + end_point
request_body = {
'UserName':'un',
'Password':'pw'
}
header = {
'tenant': 'tenant name' #required by vendor
}
print(type(base_url))
print(type(end_point))
print(type(full_url))
print(type(request_body))
print(type(header))
try:
conn = httplib.HTTPSConnection(base_url)
conn.request(method='POST',url=full_url, body=request_body, headers=header)
response = conn.getresponse()
data = response.read()
print(type(data))
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
我收到以下错误:
TypeError:无法将 str 连接到字节
完整追溯:
<class 'str'>
<class 'str'>
<class 'str'>
<class 'dict'>
<class 'dict'>
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-1a19e8736fec> in <module>
24 conn = httplib.HTTPSConnection(base_url)
---> 25 conn.request(method='POST',url=full_url, body=request_body, headers=header)
26 response = conn.getresponse()
~\Documents\Anaconda\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
1243 """Send a complete request to the server."""
-> 1244 self._send_request(method, url, body, headers, encode_chunked)
1245
~\Documents\Anaconda\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
1289 body = _encode(body, 'body')
-> 1290 self.endheaders(body, encode_chunked=encode_chunked)
1291
~\Documents\Anaconda\lib\http\client.py in endheaders(self, message_body, encode_chunked)
1238 raise CannotSendHeader()
-> 1239 self._send_output(message_body, encode_chunked=encode_chunked)
1240
~\Documents\Anaconda\lib\http\client.py in _send_output(self, message_body, encode_chunked)
1063 chunk = f'{len(chunk):X}\r\n'.encode('ascii') + chunk \
-> 1064 + b'\r\n'
1065 self.send(chunk)
TypeError: can't concat str to bytes
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-24-1a19e8736fec> in <module>
30 conn.close()
31 except Exception as e:
---> 32 print("[Errno {0}] {1}".format(e.errno, e.strerror))
AttributeError: 'TypeError' object has no attribute 'errno'
我在这里想念什么?我不明白什么?