Loli
Loli

Reputation: 11

Python Flask/JSON Error: Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)

guys.

So my problem is as follows. I created a linear model and saved it as .pkl in Python 3.7.

Then, I created an app.py file with the code shown below (html template file is also created).

import pickle
from flask import Flask, request, render_template, jsonify

#creating instance of the class
app=Flask(__name__)

#loading the model
model = pickle.load(open("model.pkl", "rb"))

#loading the index template and the main page
@app.route('/')
def index():
    return render_template('index.html')

#inputs from the user
@app.route('/result', methods=['POST'])
def result():
    features = request.get_json(force=True)['Input1', 'Input2', 'Input3', 
                                            'Input4', 'Input5', 'Input6', 
                                            'Input7', 'Input8', 'Input9']

#creating a response object
#storing the model's prediction in the object
    response = {}
    response['predictions'] = model.predict([features]).tolist()

#returning the response object as json
    return flask.jsonify(response)

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000, debug=True)

The problem is that, when I run app.py file, I get this error: "Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)". I tried modifying my code multiple times, even trying to find another solution to write it, but no luck so far, it just always raises an error.

Is there any mistake in my code that may raise this error? I should note (if it may be important) that all my variables are float type except one which is an integer.

The html script is included here as follows:

<html>
<body>
    <h3>Prediction_form</h3>

<div>
  <form action="{{ url_for('result') }}" method="POST">
    <label for="Input1">Input1</label>
    <input type="text" id="Input1" name="Input1">
    <br>
    <label for="Input2">Input2</label>
    <input type="text" id="Input2" name="Input2">
    <br>
    <label for="Input3">Input3</label>
    <input type="text" id="Input3" name="Input3">
    <br>
    <label for="Input4">Input4</label>
    <input type="text" id="Input4" name="Input4">
    <br>
    <label for="Input5">Input5</label>
    <input type="text" id="Input5" name="Input5">
    <br>
    <label for="Input6">Input6</label>
    <input type="text" id="Input6" name="Input6">
    <br>
    <label for="Input7">Input7</label>
    <input type="text" id="Input7" name="Input7">
    <br>
    <label for="Input8">Input8</label>
    <input type="text" id="Input8" name="Input8">
    <br>
    <label for="Input9">Input9</label>
    <input type="text" id="Input9" name="Input9">
    <br>
    <input type="submit" value="Submit">
    <br>
    <br>
    {{ prediction_text }}
  </form>
</div>
</body>
</html>

I am new to Python, so I may be missing something important.

Any help is appreciated.

Many thanks.

Upvotes: 0

Views: 1104

Answers (1)

Dave W. Smith
Dave W. Smith

Reputation: 24956

The form post uses the default encoding of application/x-www-form-urlencoded. That's not compatible with using get_json() to retrieve the posted form. Setting up your data for prediction will require something like this

form_fields = ['Field1', 'Field2' ... and so on ]
features = [request.form[field] for field in form_fields]

instead.

Upvotes: 1

Related Questions