Search code examples
jsonflutterdartmiddlewareapprequests

Creating a post request using dart passing the information through the request body as json data


Calling the API model

APIService apiService = new APIService();
        apiService
            .createsShowroom(autoModel)
            .then((value) {
          if (value.id != null) {
            print('Added Successfully');
            Navigator.of(context).pushNamed('/home');
          } else {
            print('not added');
          }
        });

API request

 //Create Showroom Details
  Future<AutogeneratedResponse> createsShowroom(AutogeneratedRequest autoModel) async {
    String showroomCreateUrl =
        "here is the url";
    print(autoModel.toJson());
    final res = await http.post(
      Uri.parse(showroomCreateUrl),
      body: autoModel.toJson(),
      headers: <String, String>{
        'accept': 'application/json',
        'Authorization': 'Bearer $token',
      },
    );
    if (res.statusCode == 200 || res.statusCode == 400) {
      return AutogeneratedResponse.fromJson(json.decode(res.body));
    } else {
      throw Exception('Failed to load data');
    }
  }

My Object Model

class AutogeneratedResponse{
  String website;
  String location;
  String linkedin;
  Null manufacturer;
  String locality;
  int ownerID;
  int id;
  String startTiming;
  String state;
  String name;
  String endTiming;
  String pincode;
  String email;
  String gstNumber;
  String whatsapp;
  String contactPerson;
  String daysOpen;
  String facebook;
  String telephone;
  String address;
  String instagram;
  String mobile;
  String twitter;

  AutogeneratedResponse(
      {this.website,
        this.location,
        this.linkedin,
        this.manufacturer,
        this.locality,
        this.ownerID,
        this.id,
        this.startTiming,
        this.state,
        this.name,
        this.endTiming,
        this.pincode,
        this.email,
        this.gstNumber,
        this.whatsapp,
        this.contactPerson,
        this.daysOpen,
        this.facebook,
        this.telephone,
        this.address,
        this.instagram,
        this.mobile,
        this.twitter});

  AutogeneratedResponse.fromJson(Map<String, dynamic> json) {
    website = json['website'];
    location = json['location'];
    linkedin = json['linkedin'];
    manufacturer = json['manufacturer'];
    locality = json['locality'];
    ownerID = json['ownerID'];
    id = json['id'];
    startTiming = json['start_timing'];
    state = json['state'];
    name = json['name'];
    endTiming = json['end_timing'];
    pincode = json['pincode'];
    email = json['email'];
    gstNumber = json['gst_number'];
    whatsapp = json['whatsapp'];
    contactPerson = json['contact_person'];
    daysOpen = json['days_open'];
    facebook = json['facebook'];
    telephone = json['telephone'];
    address = json['address'];
    instagram = json['instagram'];
    mobile = json['mobile'];
    twitter = json['twitter'];
  }
}


class AutogeneratedRequest{
  String name;
  String email;
  String contactPerson;
  String telephone;
  String mobile;
  String website;
  String startTiming;
  String endTiming;
  String gstNumber;
  String daysOpen;
  String address;
  String locality;
  String location;
  String state;
  String pincode;
  String whatsapp;
  String facebook;
  String instagram;
  String twitter;
  String linkedin;

  AutogeneratedRequest(
      {this.name,
        this.email,
        this.contactPerson,
        this.telephone,
        this.mobile,
        this.website,
        this.startTiming,
        this.endTiming,
        this.gstNumber,
        this.daysOpen,
        this.address,
        this.locality,
        this.location,
        this.state,
        this.pincode,
        this.whatsapp,
        this.facebook,
        this.instagram,
        this.twitter,
        this.linkedin});

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['email'] = this.email;
    data['contact_person'] = this.contactPerson;
    data['telephone'] = this.telephone;
    data['mobile'] = this.mobile;
    data['website'] = this.website;
    data['start_timing'] = this.startTiming;
    data['end_timing'] = this.endTiming;
    data['gst_number'] = this.gstNumber;
    data['days_open'] = this.daysOpen;
    data['address'] = this.address;
    data['locality'] = this.locality;
    data['location'] = this.location;
    data['state'] = this.state;
    data['pincode'] = this.pincode;
    data['whatsapp'] = this.whatsapp;
    data['facebook'] = this.facebook;
    data['instagram'] = this.instagram;
    data['twitter'] = this.twitter;
    data['linkedin'] = this.linkedin;
    return data;
  }
}

The object is created for the model and the parameters are passed to the constructor. The API is being called and throwing exception that was set for not loading the data to server, AND in server logs getting unprocessable identity which means there is some mistake while sending the request. Please have a look at it.


Solution

  • Please convert the JSON object to a String using:

    json.encode(autoModel.toJson())