Flutter Post Request to a Flask API

问题描述 投票:0回答:1

我想从我的App中发送一个请求 Post编码在Flutter中,其中有一个图像转换在Base 64。下面是我使用的代码。

Future<List<Result>> postJSON(String imageP, String iP, String port) async {

  final String jsonEndpoint = "http://$iP:$port/todo/api/v1.0/tasks/mdrv";

  final response = await http.post('$jsonEndpoint', body:
  {
    "id": "3",
    "title_image": "Test",
    "b64Image": "$imageP",
    "done": "false",
  },);

    if (response.statusCode == 200){
      List results = jsonDecode(response.body);
      return results
          .map(
              (result) => new Result.fromJson(result))
          .toList();
    } else {
      throw Exception('Erreur dans le chargement, veuillez réessayer');
    }
 }

但是,当我做的请求,我有以下TypeError对我的Flask API 。

description = JSON["b64Image"] 
TypeError: 'NoneType' object is not subscriptable

我正在使用下面的Python代码。

def send_client():
  Lresult_algo=[]
  JSON = request.get_json()         
  id = JSON['id']
  'description':JSON['b64Image']  
  server=Server(description)       
  description1=server.B64_array(description)
  description2=Image.fromarray(description1)
  description2.save(r"C:\Users\vince\Desktop\test2.png")
  queryPath=r"C:\Users\vince\Desktop\test2.png"
  Lresult_algo=server.send(queryPath)
  maskedBodies_b64 = []
  for matrice in Lresult_algo:
      matrice1=matrice.astype('uint8')
      maskedBodies_b64.append(base64.b64encode(cv2.imencode('.jpg', matrice1)[1]))
  maskedBodies_b64=[str(b64) for b64 in maskedBodies_b64]
  data = {
        'Image_1' : maskedBodies_b64[0],
        'Image_2' : maskedBodies_b64[1],
        'Image_3' : maskedBodies_b64[2],
        'Image_4' : maskedBodies_b64[3],
        'Image_5' : maskedBodies_b64[4]
          }
  resp=json.dumps(data)
  return resp

你认为这是一个打字的问题吗?我怎样才能解决这个问题呢?

python flutter flask post request
1个回答
0
投票

我把我的代码改成这样,但还是有同样的错误。

Future<List<Result>> postJSON(String imageP, String iP, String port) async {

  final String jsonEndpoint = 'http://$iP:$port/api/v1.0/tasks/mdrv';

  Map<String, dynamic> data = {
    'id': 1,
    'title_image': "Test",
    'b64Image': "$imageP",
    'done': false,
  };

  var client = new http.Client();

  var body = jsonEncode(data);

  var response = await client.post('$jsonEndpoint',headers: {"Content-Type": "application/json"}, body: body,);

    if (response.statusCode == 200){
      List results = jsonDecode(response.body);
      return results
          .map(
              (result) => new Result.fromJson(result))
          .toList();
    } else {
      throw Exception('Erreur dans le chargement, veuillez réessayer');
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.