首页 > 解决方案 > 为什么发布到烧瓶应用程序的二进制数据不同?

问题描述

我将二进制数据(例如 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'

标签: pythonflaskpython-requests

解决方案


推荐阅读