0

I'm building my first API with Aqueduct and a PostgreSQL db. To test my endpoint I'm using Postman on the locally running server. GET requests works as expected, but the POST request fails with missing Body error:

{
    "error": "missing required Body ''"
}

this is the method:

@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();
  }

and this is the Model:

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,nullable: true, indexed: true)
  String country;
  @Column(unique: false, nullable: true, indexed: true)
  int date;
  @Column(unique: false, nullable: true, indexed: true)
  String description;
  @Column(unique: false, nullable: true, indexed: true)
  String alertIcon;
  @Column(unique: false, nullable: true, indexed: true)
  String latitude;
  @Column(unique: false, nullable: true, indexed: true)
  String longitude;
  @Column(unique: false, nullable: true, indexed: true)
  String alertImageName;
  @Column(unique: false, nullable: true, indexed: true)
  String alertImageUrl;
  @Column(unique: false, nullable: true, indexed: true)
  String userName;
  @Column(unique: false, nullable: true, indexed: true)
  int utilityPoints;
  @Column(unique: false, nullable: true, indexed: true)
  int votesToDelete;
}

In Postman I set the host as localhost:8888/alerts as for the Get requests, selected a Content-Type application/json header and a raw JSON Body which is :

{
    "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
}

Can you spot why is missing the Body? Many thanks.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Vincenzo
  • 5,304
  • 5
  • 38
  • 96

1 Answers1

0

Finally it was a Postman headers setting problem.. I in did only enable `Content-Type application/json and wrongly disabled the others. They had to be active too and it now works as expected. Hope this helps others. Cheers.

enter image description here

Vincenzo
  • 5,304
  • 5
  • 38
  • 96