1

I have created FLASK web service to upload an image but I'm getting an error when I try to call it using POSTMAN.

Getting an error

400 Bad Request: KeyError: 'file1'

But I am sure I have same key name at both the places.

My Code:

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)

This is how I am calling the web service using postman

Postman Image

Headers

enter image description here

How can I resolve this?

17
  • 1
    Try adding if request.method=='POST' as first line of function Commented May 16, 2019 at 18:19
  • 1
    Just for sake of confirming. Use curl.. curl -X POST -F file1=@"/path/to/my/file/a.jpg" localhost:5000/img Commented May 16, 2019 at 18:47
  • 1
    @TechatTheSparksFoundation Strange ...getting response here. Commented May 16, 2019 at 18:49
  • 1
    Yeah probably the postman isnt sending correct request. It appends temp headers which modifies your request and stops it from processing. Your code will work fine once you integrate it to front end. :) Commented May 16, 2019 at 18:50
  • 1
    Thanks for your time and efforts :-) Commented May 16, 2019 at 18:51

1 Answer 1

2

Postman might be sending additional headers with your request. Just for sake of testing your code use curl..

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

Probably the postman isn't sending the correct request. It appends temp headers which modifies your request and stops it from processing. Your code will work fine once you integrate it to the front end.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.