对FlaskAPI的颤振Post请求

2024-09-29 22:00:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从我的应用程序中发送一个请求帖子,代码为flatter,其中有一个在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');
    }
 }

但是,当我执行请求时,我的Flask API上有以下TypeError:

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

你认为这是打字问题吗?我怎样才能修好它


Tags: 代码imageidjsonstringserverresponsedescription
1条回答
网友
1楼 · 发布于 2024-09-29 22:00:03

我这样更改了代码,但仍然存在相同的错误:

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');
    }
}

相关问题 更多 >

    热门问题