使用 dart 创建一个 post 请求,将信息作为 json 数据通过请求主体传递

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

调用 API 模型

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请求

 //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');
    }
  }

我的对象模型

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;
  }
}

为模型创建对象并将参数传递给构造函数。 正在调用 API 并抛出设置为不将数据加载到服务器的异常,并且在服务器日志中获取无法处理的标识,这意味着发送请求时存在一些错误。请看一下。

请使用以下方法将 JSON 对象转换为 String

json.encode(autoModel.toJson())