How to convert to bytes when TypeError: can't concat bytes to str
How to convert to bytes when TypeError: can't concat bytes to str
我正在尝试通过 API 发送数据,但收到 TypeError: can't concat bytes to str.据我所知,这意味着我需要将部分代码转换为字节,但我不确定如何执行此操作。我试过在前面添加 b 或使用 bytes('data') 但可能将它们放在错误的区域。
import http.client
conn = http.client.HTTPSConnection("exampleurl.com")
payload = {
'FilterId': "63G8Tg4LWfWjW84Qy0usld5i0f",
'name': "Test",
'description': "Test1",
'deadline': "2017-12-31",
'exclusionRuleName': "Exclude",
'disable': "true",
'type': "Type1"
}
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'x-csrf-token': "wWjeFkMcbopci1TK2cibZ2hczI",
'cache-control': "no-cache",
'postman-token': "23c09c76-3b030-eea1-e16ffd48e9"
}
conn.request("POST", "/api/campaign/create", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
这是问题行:
conn.request("POST", "/api/campaign/create", payload, headers)
我不确定什么以及如何转换为字节。
如果可以的话,使用requests
,它更容易使用。
否则,您需要urlencode
将有效负载发布到服务器。您的负载的 url 编码版本如下所示:
description=Test1&exclusionRuleName=Exclude&FilterId=63G8Tg4LWfWjW84Qy0usld5i0f&deadline=2017-12-31&type=Type1&name=Test&disable=true
这是一个工作示例:
import http.client
from urllib.parse import urlencode
conn = http.client.HTTPSConnection("httpbin.org")
payload = {
'FilterId': "63G8Tg4LWfWjW84Qy0usld5i0f",
'name': "Test",
'description': "Test1",
'deadline': "2017-12-31",
'exclusionRuleName': "Exclude",
'disable': "true",
'type': "Type1"
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'x-csrf-token': "wWjeFkMcbopci1TK2cibZ2hczI",
'cache-control': "no-cache",
'postman-token': "23c09c76-3b030-eea1-e16ffd48e9"
}
conn.request("POST", "/post", urlencode(payload), headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
http://httpbin.org returns 这个 JSON 回复:
{
"args": {},
"data": "",
"files": {},
"form": {
"FilterId": "63G8Tg4LWfWjW84Qy0usld5i0f",
"deadline": "2017-12-31",
"description": "Test1",
"disable": "true",
"exclusionRuleName": "Exclude",
"name": "Test",
"type": "Type1"
},
"headers": {
"Accept-Encoding": "identity",
"Cache-Control": "no-cache",
"Connection": "close",
"Content-Length": "133",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"Postman-Token": "23c09c76-3b030-eea1-e16ffd48e9",
"X-Csrf-Token": "wWjeFkMcbopci1TK2cibZ2hczI"
},
"json": null,
"origin": "220.233.14.203",
"url": "https://httpbin.org/post"
}
请注意,我使用的是 httpbin.org as a test server, posting to https://httpbin.org/post。
此外,我已将 Content-type header 更改为 application/x-www-form-urlencoded,因为这是 urlencode() 返回的格式。
我正在尝试通过 API 发送数据,但收到 TypeError: can't concat bytes to str.据我所知,这意味着我需要将部分代码转换为字节,但我不确定如何执行此操作。我试过在前面添加 b 或使用 bytes('data') 但可能将它们放在错误的区域。
import http.client
conn = http.client.HTTPSConnection("exampleurl.com")
payload = {
'FilterId': "63G8Tg4LWfWjW84Qy0usld5i0f",
'name': "Test",
'description': "Test1",
'deadline': "2017-12-31",
'exclusionRuleName': "Exclude",
'disable': "true",
'type': "Type1"
}
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'x-csrf-token': "wWjeFkMcbopci1TK2cibZ2hczI",
'cache-control': "no-cache",
'postman-token': "23c09c76-3b030-eea1-e16ffd48e9"
}
conn.request("POST", "/api/campaign/create", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
这是问题行:
conn.request("POST", "/api/campaign/create", payload, headers)
我不确定什么以及如何转换为字节。
如果可以的话,使用requests
,它更容易使用。
否则,您需要urlencode
将有效负载发布到服务器。您的负载的 url 编码版本如下所示:
description=Test1&exclusionRuleName=Exclude&FilterId=63G8Tg4LWfWjW84Qy0usld5i0f&deadline=2017-12-31&type=Type1&name=Test&disable=true
这是一个工作示例:
import http.client
from urllib.parse import urlencode
conn = http.client.HTTPSConnection("httpbin.org")
payload = {
'FilterId': "63G8Tg4LWfWjW84Qy0usld5i0f",
'name': "Test",
'description': "Test1",
'deadline': "2017-12-31",
'exclusionRuleName': "Exclude",
'disable': "true",
'type': "Type1"
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'x-csrf-token': "wWjeFkMcbopci1TK2cibZ2hczI",
'cache-control': "no-cache",
'postman-token': "23c09c76-3b030-eea1-e16ffd48e9"
}
conn.request("POST", "/post", urlencode(payload), headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
http://httpbin.org returns 这个 JSON 回复:
{ "args": {}, "data": "", "files": {}, "form": { "FilterId": "63G8Tg4LWfWjW84Qy0usld5i0f", "deadline": "2017-12-31", "description": "Test1", "disable": "true", "exclusionRuleName": "Exclude", "name": "Test", "type": "Type1" }, "headers": { "Accept-Encoding": "identity", "Cache-Control": "no-cache", "Connection": "close", "Content-Length": "133", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "Postman-Token": "23c09c76-3b030-eea1-e16ffd48e9", "X-Csrf-Token": "wWjeFkMcbopci1TK2cibZ2hczI" }, "json": null, "origin": "220.233.14.203", "url": "https://httpbin.org/post" }
请注意,我使用的是 httpbin.org as a test server, posting to https://httpbin.org/post。
此外,我已将 Content-type header 更改为 application/x-www-form-urlencoded,因为这是 urlencode() 返回的格式。