python - 为什么发布到烧瓶应用程序的二进制数据不同?
问题描述
我将二进制数据(例如 pdf 文件)发布到烧瓶应用程序。我希望服务器接收到的数据与我发布的数据相匹配,但事实并非如此。我猜它可能与编码有关,但我不知道该去哪里。
编辑:客户端在 MacOS Mojave 上运行,服务器在 AWS Lambda 上运行
客户端代码如下所示:
import requests
import os
data = None
with open("infile.pdf", "rb") as infile:
data = infile.read()
print("length of input data: {}".format(len(data)))
print("first 40: {}".format(data[:40]))
the_url = os.environ['BASE_DOMAIN'] + "/binary"
res = requests.post(url=the_url,
data=data,
headers={'Content-Type': 'application/octet-stream'})
输出如下所示:
12:14 $ python tester.py
length of input data: 1728914
first 40: b'%PDF-1.5\n%\xe4\xf0\xed\xf8\n4 0 obj\n<</Type/XObject/S'
服务器代码如下所示:
@app.route("/binary", methods=["POST"])
def create_binary():
print(request.content_type)
if request.content_type == "application/octet-stream":
print("content length header: {}".format(request.content_length))
data = request.get_data()
print("length of input data: {}".format(len(data)))
print("first 40: {}".format(data[:40]))
return jsonify({
'msg': "success"
})
else:
return jsonify({
'msg': "415 Unsupported Media Type ;)"
})
它的输出如下所示:
application/octet-stream
content length header: 3118403
length of input data: 3118403
first 40: b'%PDF-1.5\n%\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\n4 0 obj\n<</Type/X'
解决方案
推荐阅读
- java - 迁移前的 Flyway 回调
- javascript - 如何访问 mapDispatchToProps 中 mapStateToProps 添加的属性?
- php - Sage 50 ODBC 上 PHP 连接字符串语法
- javascript - How is the element passed into the callback method in JavaScript?
- python - 将值存储在动态创建的变量名中
- bash - 如果“if”为假,如何继续脚本?多个“其他”
- c++ - 无法将参数 1 从 'std::string' 转换为 'std::string &&'
- xamarin.forms - 您真的应该将图像的三个副本放在 Xamarin.Forms 项目中吗?
- javascript - 将 props 从子组件传递到主组件
- hyperledger-fabric - 在区块链中保存文件的最佳实践