我正在嘗試發出 POST 請求,但我不斷收到錯誤 400 Bad Request。在郵遞員中,我有這樣的原始身體:
[
{
"athleteId":"16198",
"departmentId":"3",
"teamId":"278"
}
]
API 請求
static Future<List<Athlete>> insertPresences(
int athleteId, int departmentId, int teamId) async {
try {
final response = await http.post(
Uri.parse('https://my_domain/path'),
headers: <String, String>{
'Authorization': 'Basic ..',
'Content-Type': 'application/json; charset=UTF-8',
'Accept': 'application/json'
},
body: jsonEncode({
"athleteId": athleteId,
"departmentId": departmentId,
"teamId": teamId
}),
);
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
if (response.statusCode == 201) {
List jsonResponse = json.decode(utf8.decode(response.bodyBytes));
return jsonResponse
.map((_insert) => Athlete.fromJson(_insert))
.toList();
}
} catch (e) {
logger.e(e.toString());
}
return insertPresences(athleteId, departmentId, teamId);
}
但我收到錯誤:
Response status: 400
I/flutter (20754): Response body: {"timestamp":"2022-10-28T12:46:39.969 00:00","status":400,"error":"Bad Request","path":".."}
如果有人可以向我解釋我做錯了什么,我將不勝感激!
uj5u.com熱心網友回復:
您缺少原始正文中顯示的外部括號。只需添加括號,例如
body: jsonEncode([{
"athleteId": athleteId,
"departmentId": departmentId,
"teamId": teamId,
}]),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/522601.html
標籤:扑镖
上一篇:闡明dart中的異步操作
