'Flask POST Method Not being Allowed. Not allowed on the URL

I've been trying to push some data which was fed to a machine learning and been trying to show the results on screen however no matter what I try I can't seem to be able to dodge this error.

app = Flask(__name__)

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


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


def predict():

   
 if request.method == 'POST' or 'GET':
        try:
          json_ = request.json
          print(json_)
          query = pd.get_dummies(pd.DataFrame(json_))
          query = query.reindex(columns= None, fill_value=0)
          classifier = joblib.load('./pkl/Without.pkl')
          prediction = classifier.predict(query)
          if prediction == 0:
                 pred_text = 'Rejected'
          else:
                 pred_text = 'Approved'
          #return jsonify({'prediction': list(prediction)})
          return jsonify(result={"status": 200})
        except:
               traceback.print_exc(file = sys.stdout)
 else:
        return "none ff"
          
if __name__ == '__main__':
     app.run()

Does anyone know why the "Method" is not being allowed?



Solution 1:[1]

you have a missing single quote at:

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

the decorator for /predict is commented. I stripped down your code to this block:

from flask import Flask, request

app = Flask(__name__)

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


@app.route('/predict', methods=['POST', 'GET'])
def predict():
    if request.method == 'POST' or 'GET':
        return "request type: {}".format(request.method)

if __name__ == '__main__':
     app.run(debug=True, host="0.0.0.0", port=8000)

and this works for both GET and POST requests.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ilias-sp