如何在Postman中上传文件?

3

我想通过POSTMAN的POST方法传递图片,但在请求文件行中得到了None类型的响应request.files['image']。

我尝试了许多不同的方法,但是我无法解决这个问题。

from flask import Flask,jsonify
from flask import request
import face_recognition
import json

app = Flask(__name__)

@app.route('/')
def index():
   return ''


@app.route('/valid_faces', methods=['POST'])


def POST():
    if request.method == 'POST':

    # getting the images url from the request
    print('....')
    name1 = request.files['file'] if request.files.get('file') else None
    print('....')
    print (name1)        # name2 = request.form.get('name2')

    # map the url to the ones in the folder images
    firstImage = name1


    # loading the image inside a variable
    firstImage = face_recognition.load_image_file(firstImage)




    result=face_recognition.face_locations(firstImage)


    if result:
        # x={'valid_faces':True}
        # return jsonify(x)
        return "True"
    else:
        # x={'valid_faces':False}
        # return jsonify(x)
        return "False"


if __name__ == "__main__":
    app.run(debug=True)

尝试打印request以调查其中的内容。 - Vaibhav Vishal
你做了哪些故障排除工作? - dfundako
你是如何上传文件的?Postman中的请求是什么样子的? - Danny Dainton
我正在使用POSTMAN将文件以'图像'作为键,文件作为值通过form-data进行发布。 - Bivek Gyawali
5个回答

8

如果您想将文件附加到Postman调用,则需要将其添加到正文中。 请检查form-data并选择file而不是text。现在,您可以使用Windows文件资源管理器选择文件。

要获取给定的文件,请使用request软件包。

file = request.files['file']

索引必须与您在Postman请求体数据中指定的字符串相同。

属性错误:'NoneType'对象没有属性'read' / 我遇到了这个错误。数据没有从POSTMAN传递到Flask应用程序。 - Bivek Gyawali
@BivekGyawali 你能给我提供你的模块导入吗? - ZKDev

3
在Postman中,按照以下步骤进行操作:
  • 将方法设置为POST
  • 将body类型设置为form-data
  • 为输入建立键/值对。将Key的值设置为“file”
  • 将鼠标悬停在Key字段上,从下拉菜单中选择Text或File
  • 点击发送以处理请求

1

按照以下步骤进行:https://istack.dev59.com/GGm4I.webp

  • 将方法设置为POST
  • 将body选项卡更改为form-data
  • 设置键
  • 在值中,悬停,您可以看到文件/文本。在此处选择文件
  • 选择要上传的文件。
  • 发送请求。

0

你能做到

 files = request.files.getlist("file")

你为什么喜欢这种语法胜过其他答案提供的语法?在特定情况下使用它会有什么好处吗? - Jeremy Caney
我现在正在工作的项目中,我编写的其他方法都是有效的。当我按照那样做时,它开始正常工作了。 - Khanbala Rashidov

0
在我的情况下,Postman为请求中的`request.files`对象设置了一个空的第一个值。您可以使用以下方法进行检查:
print(str(request.files))

而且如果索引为空

file = request.files['file']

尝试使用
file = request.files['']

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接