Flask Web服务中的KeyError

时间:2019-05-16 18:13:25

标签: python flask postman

我已经创建了FLASK Web服务来上传图像,但是尝试使用POSTMAN进行调用时出现错误。

遇到错误

400 Bad Request: KeyError: 'file1'

但是我确定我在两个地方都有相同的密钥名称。

我的代码:

from flask import Flask, request
from scipy.stats import wasserstein_distance
import numpy as np
import cv2
from imutils import paths
from werkzeug.utils import secure_filename

app = Flask(__name__)


@app.route("/img", methods=['POST'])
def search():

    file = request.files['file1']
    filename = secure_filename(file.filename)
    file.save(filename)

    images = list(paths.list_images("data"))

    query_image = cv2.imread(filename)
    query_image = cv2.cvtColor(query_image, cv2.COLOR_BGR2GRAY)
    q_hist = get_histogram(query_image)

    hist = []

    for i in images:
        image = cv2.imread(i)

        # convert the images to grayscale
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        s_hist = get_histogram(image)
        dist = wasserstein_distance(q_hist, s_hist)
        hist.append(dist)

    x = min(hist)
    print(hist)
    if x > 2:
        return "Image Not Found"
    else:
        idx = hist.index(x)
        matched_img = images[idx]
        return des[matched_img]


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

这就是我使用邮递员调用Web服务的方式

Postman Image

标题

enter image description here

我该如何解决?

1 个答案:

答案 0 :(得分:1)

邮递员可能会随您的请求发送其他标头。 只是为了测试您的代码,请使用curl。

  

curl -X POST -F file1 = @“ / path / to / my / file / a.jpg”   http://localhost:5000/img

邮递员发送的请求可能不正确。它附加了临时标头,这些标头会修改您的请求并停止处理。将代码集成到前端后,您的代码将可以正常工作。