Search code examples
pythonflaskflask-restful

getting error in flask while running the app


I am building a rest API using flask and when I run I am getting The method is not allowed for the requested URL. I don't know where I am doing wrong

@app.route('/predict', methods=["POST"])
def predict():
    solute = request.form.get("solute")
    solvent = request.form.get("solvent")
    results = predictions(solute, solvent)

    response = {}
    response["response"] = {
        'energy': str(results)
    }
    return flask.jsonify(response)



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

Here solute and solvent take data and results are in the form of a floating point in json format


Solution

  • Could you told us wich flask version are you using and which Python interpreter ? I copy paste your API adding predictions method and library for flask and it run perfectly without this error message.

    Perhaps this error is trigger by another route ? Are u sure it come from this part ?

    Here te entire code that i use to trigger this error but nothing happen.

    from flask import Flask, request
    from flask.json import jsonify
     
    app = Flask(__name__)
    
    def predictions(solute, solvent):
        res = float(solute) + float(solvent)
        return res
    
    @app.route('/predict', methods=["POST"])
    def predict():
    
        solute = request.form.get('solute')
        solvent = request.form.get('solvent')
        result = predictions(solute, solvent)
    
        response = {}
        response["response"] = {
            'energy': str(result)
        }
        return jsonify(response)
    
    
    if __name__ == '__main__':
        app.run(debug=True, host="0.0.0.0", port=3000)
    

    I'm using POSTMAN to send the POST request with data inside Body as form-data, please make sure you send correct query, perhaps the error come from here.

    Return query with solute = 2.3 and solvent = 11.5

    {
        "response": {
            "energy": "13.8"
        }
    }