如何解决POST 请求失败,缺少必需的 Body Aqueduct + PostgreSQL
我正在使用 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
}
你能发现为什么缺少身体吗? 非常感谢。
解决方法
最后是 Postman headers 设置问题.. 我只启用了`Content-Type application/json 并错误地禁用了其他的。 他们也必须保持活跃,现在它按预期工作。 希望这对其他人有帮助。 干杯。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。