我收到一个错误:预期的str实例,当我尝试将图像二进制添加到多部分/表单数据时发现的字节。
问题是我试图将二进制格式的图像数据附加到字符串中。有没有办法将二进制图像添加到多部分/表单数据中?
我处于智慧的尽头,希望得到一些帮助。
imageData = request.FILES['filePath'].read()
content_type, request_body = encode_multipart_formdata([('include_target_data', targetMetaDataRet),
('max_num_results', str(maxNoResultRet))],
[('image', imagePath, imageData)])
def encode_multipart_formdata(fields, files):
BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
CRLF = 'rn'
lines = []
for (key, value) in fields:
lines.append('--' + BOUNDARY)
lines.append('Content-Disposition: form-data; name="%s"' % key)
lines.append('')
lines.append(value)
for (key, filename, value) in files:
lines.append('--' + BOUNDARY)
lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
lines.append('Content-Type: %s' % get_content_type(filename))
lines.append('')
lines.append(value)
lines.append('--' + BOUNDARY + '--')
lines.append('')
body = CRLF.join(lines)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body
追踪:
35. response = get_response(request)
128. response = self.process_exception_by_middleware(e, request)
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
166. [('image', imagePath, imageData)])
232. body = CRLF.join(lines)
Exception Type: TypeError at /identify_shrine
Exception Value: sequence item 12: expected str instance, bytes found
根据@coltoneakins请求,我将请求正文修改为字节,但我似乎收到一个错误的请求错误,知道为什么吗?
法典:
content_type = 'multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$'
request_body = '----------ThIs_Is_tHe_bouNdaRY_$' + 'n'+'Content-Disposition: form-data; name="include_target_data"' + 'n' + 'n' + 'top'+ 'n' + '----------ThIs_Is_tHe_bouNdaRY_$' +'n' + 'Content-Disposition: form-data; name="max_num_results"' + 'n' + 'n' + '1' + 'n' + '----------ThIs_Is_tHe_bouNdaRY_$' +'n' + 'Content-Disposition: form-data; name="image"; filename="img_2.jpg"' + 'n' + 'Content-Type: image/jpeg' + 'n' + 'n'
request_body1 = request_body.encode('utf-8')
request_body2 = imageData
request_body3 = ('n' + 'n' + '----------ThIs_Is_tHe_bouNdaRY_$').encode('utf-8')
request_body4 = request_body1 + request_body2 + request_body3
content_type_bare = 'multipart/form-data'
# Sign the request and get the Authorization header
# use client key
auth_header = authorization_header_for_request(CLIENT_ACCESS_KEY, CLIENT_SECRET_KEY, HTTP_METHOD, request_body4,
content_type_bare,
date, path)
request_headers = {
'Accept': 'application/json',
'Authorization': auth_header,
'Content-Type': content_type,
'Date': date
}
try:
# Make the request over HTTPS on port 443
connection = http.client.HTTPSConnection(CLOUD_RECO_API_ENDPOINT, 443)
connection.request(HTTP_METHOD, path, request_body4, request_headers)
response = connection.getresponse()
response_body = response.read()
reason = response.reason
status = response.status
finally:
connection.close()
您的代码中存在类型问题。您正在获得一个TypeError 预期的 str 实例,找到的字节数是因为您正在尝试加入((一个包含Python 中的 str类型和字节类型的列表。
查看代码中的以下行:
for (key, filename, value) in files:
lines.append('--' + BOUNDARY)
lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
lines.append('Content-Type: %s' % get_content_type(filename))
lines.append('')
lines.append(value) # <---------- THIS IS BYTES, EVERYTHING ELSE IS STR
lines.append('--' + BOUNDARY + '--')
lines.append('')
body = CRLF.join(lines) # <---------- AHHH RED FLAG!!!
CRLF是str型。但是,值(添加到行列表中(是字节。这意味着您最终会得到同时包含str和字节类型的行。当您通过多部分/表单数据请求发送图像时,请求的整个正文都是字节。因此,您只需要将 join(( 与字节类型一起使用。
这是你正在做的:
body = CRLF.join(lines)
这真的是:
'rn, i am a str'.join(['i am also str', b'I am not a str, I am bytes']) # <-- NO
这是您需要做的:
b'I am bytes'.join([b'I am also bytes', b'Me too!'])
此外,为了让您知道,请求库提供了发送文件的机制。请参阅请求文档中的 files 参数或此 StackOverflow 答案:
https://stackoverflow.com/a/12385661/9347694
因此,您可能不需要在这里重新发明轮子。请求将对文件进行分段编码并为您构造请求。