【发布时间】:2020-06-12 20:59:58
【问题描述】:
我想从我用 Flutter 编码的应用程序发送一个请求 Post,其中有一个用 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
您认为这是打字问题吗?我该如何解决?
【问题讨论】:
-
看起来您的 API 需要 JSON 编码的正文,但您发送的是表单编码的正文。尝试对地图进行编码并将该字符串用作正文。请注意,现在您是 JSON 编码,整数和布尔值可能不再是字符串。
-
我确实更改了我的代码并为正文添加了一个 json.encode 但仍然存在相同的 TypeError
-
似乎 API 认为我发送的 JSON 为空
标签: python flutter flask post request