Home > Mobile >  Flutter not sending post request to flask server
Flutter not sending post request to flask server

Time:08-18

I am trying to send a form data through flutter to my flask webserver but the flask server is not receiving the request. Below is my flutter code

 class FetchData {
  Future<Price> getData(
    int area,
    int bedrooms,
    double? psf,
    int feature_score,
    double? locationPremium,
  ) async {
    const uri = 'http://127.0.0.1:5000/predict';
    var map = <String, dynamic>{};
    map['area'] = area;
    map['bedrooms'] = bedrooms;
    map['psf'] = psf;
    map['feature_score'] = feature_score;
    map['location_premium'] = locationPremium;
    map['log_premium'] = log(locationPremium!);
    http.Response response = await http.post(Uri.parse(uri), body: map);
    if (response.statusCode == 200) {
      return Price.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Failed to load post');
    }
  }
}

And below is my app.py code

from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
import pickle
import numpy as np
model=pickle.load(open('model1.pkl','rb'))
app = Flask(__name__)
@app.route('/')
def home():
    return "Hello World"
@app.route('/predict',methods=['POST'])    
def predictor():
    area=int(request.form['area'])
    bedrooms=int(request.form['bedrooms'])
    psf=float(request.form['psf'])
    feature_score=int(request.form['feature_score'])
    LocationPremium=float(request.form['LocationPremium'])
    LogPremium=float(request.form['LogPremium'])
    query=np.array([[area,bedrooms,psf,feature_score,LocationPremium,LogPremium]])
    result= model.predict(query)[0]
    return jsonify({'prediction':str(result)}) 
  
                     
if __name__ == '__main__':
    app.run(debug=True)
    app.run(host='0.0.0.0', port=5000)

I have tried sending the request through postman and the server is receiving the POST request

CodePudding user response:

You need to add headers to your post request.

Future  post(   Uri url, {   Map ? headers,   Object? body,   Encoding? encoding, }) 
  • Related