问题描述
我正在使用 Aqueduct 和 Postgresql 数据库构建我的第一个 API。为了测试我的端点,我在本地运行的服务器上使用 Postman。 GET 请求按预期工作,但 POST 请求因缺少正文错误而失败:
{
"error": "missing required Body ''"
}
这是方法:
@Operation.post()
Future<Response> adDalert(@Bind.body(ignore: ['id']) Alert newAlert) async {
print('incoming alert to save is $newAlert');
final query = Query<Alert>(context)
..values = newAlert;
final alert = await query.insert();
// return Response.ok(alert);
return alert != null
? Response.ok(alert)
: Response.badRequest();
}
这是模型:
class Alert extends Managedobject<_Alert> implements _Alert{}
class _Alert {
@primaryKey
int id;
@Column(unique: false)
String name;
@Column(unique: false,nullable: true,indexed: true)
String city;
@Column(unique: false,nullable: true,indexed: true)
String region;
@Column(unique: false,indexed: true)
String country;
@Column(unique: false,indexed: true)
int date;
@Column(unique: false,indexed: true)
String description;
@Column(unique: false,indexed: true)
String alertIcon;
@Column(unique: false,indexed: true)
String latitude;
@Column(unique: false,indexed: true)
String longitude;
@Column(unique: false,indexed: true)
String alertimageName;
@Column(unique: false,indexed: true)
String alertimageUrl;
@Column(unique: false,indexed: true)
String userName;
@Column(unique: false,indexed: true)
int utilityPoints;
@Column(unique: false,indexed: true)
int VotesToDelete;
}
在 Postman 中,对于 Get 请求,我将主机设置为 localhost:8888/alerts
,选择了一个 Content-Type application/json
标头和一个 raw JSON
正文:
{
"name":"postman","city":"Bologna","region":"Emilia-Romagna","country":"Italy","date":1111111,"description":"test","alertIcon":"test","latitude":"11.111","longitude":"22.222","alertimageName":"test","alertimageUrl":"jjj","userName":"user","utilityPoints":1,"VotesToDelete":1
}
你能发现为什么缺少身体吗? 非常感谢。