Home > database >  Api request returns 415 but it doesnt have media files
Api request returns 415 but it doesnt have media files

Time:08-18

I try to add an address to a server with this API, but the problem is it's returning an Error 415 message in the images, but I don't send any media files, I just send a JSON map like shown in the images as well.

although it works in the postman it returns this error in the emulator.

this is the function of the API request

  Future addAddress(Placemark placemark) async {
try {
  String token = DataSaver.getData(tokenKey);

  Uri uri = Uri.parse("$url$usersTag/$uid$addressTag");
  var response = await http
      .post(
    uri,
    headers: <String, String>{
      'userId': uid,
      "Authorization": "Bearer $token",
    },
    body: json.encode(AddressCredintials.fromPlacemark(placemark)),
  )
      .timeout(timeoutDuration, onTimeout: () {
    log("request TimeOut");
    return http.Response('Error', 408);
  });

  print(response.body.toString());
  if (response.statusCode == 200) {
    log("address added");
  } else {
    print(response.statusCode);
  }
} catch (e) {
  print(e);
} }

this is the class I am using.

class AddressCredintials {
  String govID;
  String cityID;
  String? villageID;
  String street;
  String? building;
  String? apartment;

  AddressCredintials(
      {this.apartment,
      this.building,
      required this.street,
      required this.cityID,
      required this.govID,
      this.villageID});

  factory AddressCredintials.fromPlacemark(Placemark placemark) {
    return AddressCredintials(
        street: placemark.street,
        cityID: placemark.city.id,
        govID: placemark.government.id,
        apartment: placemark.apartment,
        building: placemark.building,
        villageID: placemark.village != null ? placemark.village!.id : null);
  }

  Map toJson() {
    return {
      "governorateId": govID,
      "cityId": cityID,
      "areaId": villageID,
      "street": street,
      "buildingNo": building,
      "appartmentNo": apartment
    };
  }
}

CodePudding user response:

Add to the header this value "content-type": "application/json"

and in case your response is also a json add this "accept": "application/json"

  • Related