[英]TypeError: can't concat str to bytes in HTTPSConnection
我正在嘗試發出授權請求並提出TypeError: can't concat str to bytes
。 我搜索了 web 並發現了類似的問題,但沒有一個能幫助解決我的問題。 我目前在 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))
我收到以下錯誤:
類型錯誤:無法將 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'
我在這里錯過了什么? 我不明白什么?
我認為這個錯誤是因為request
方法中的body=request_body
。
conn.request(method='POST',url=full_url, body=request_body, headers=header)
從文檔
body
可以是一個 str,一個類似字節的 object,一個打開的文件 object,或者一個可迭代的字節。
但在這種情況下, request_body
是一個字典 object。因此轉換為 JSON 可能會解決這個問題。
import json
conn.request(method='POST',url=full_url, body=json.dumps(request_body), headers=header)
NameError Traceback (most recent call last)
<ipython-input-3-75a4126e3e42> in <module>
19 print(type(end_point))
20 print(type(full_url))
---> 21 print(type(body_info))
22 print(type(header))
23
NameError: name 'body_info' is not defined
您忘記定義“body_info”,或者很可能您將其定義為“request_body”但不記得了。
修復這條線就足夠了:
body_info = {
'UserName':'un',
'Password':'pw'
}
而不是命名字典request_body
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.