为什么在python中使用带有post的表单发送图像不起作用

为什么在python中使用带有post的表单发送图像不起作用,python,forms,post,flask,python-requests,Python,Forms,Post,Flask,Python Requests,正在尝试使用请求包将图像发送到Flask服务器,使用表单和发布。但是flask服务器无法解析密钥图像。如何使用表单以适当的格式发送图像和请求 Flask server.py @app.route('/api/<model_name>/predict/', methods=['POST']) def predict(model_name): if "image" in request.files.keys(): return jsonify({"msg": "

正在尝试使用请求包将图像发送到Flask服务器,使用
表单
发布
。但是flask服务器无法解析密钥
图像
。如何使用表单以适当的格式发送图像和请求

Flask server.py

@app.route('/api/<model_name>/predict/', methods=['POST'])
def predict(model_name):

    if "image" in request.files.keys():
        return jsonify({"msg": "key found"})

    print("image", request.files)
    return str(), 200
def get_predictions(path):

   url = "http://localhost:9020/api/fasterrcnn/predict/"

   payload = {"image": (path.name, open(path, "rb"), "image/jpeg")}
   headers = {'content-type': "multipart/form-data"}
   response = requests.post(
       url, data=payload, headers=headers, stream=True)

   pprint(response.text)
有人能告诉我可能的原因和解决办法吗?如果我遗漏了任何内容,过度强调或不充分强调某一特定点,请在评论中告诉我。

请求
说明您应该使用
文件=
参数发布多部分文件

例如:

导入请求
def get_预测(路径):
url=”http://localhost:9020/api/fasterrcnn/predict/"
文件={“image”:(path.name,open(path,“rb”),“image/jpeg”)}
response=requests.post(url,files=files)
pprint(response.text)