使用 dart 创建发布请求,将信息作为 json 数据通过请求正文传递

Posted

技术标签:

【中文标题】使用 dart 创建发布请求,将信息作为 json 数据通过请求正文传递【英文标题】:Creating a post request using dart passing the information through the request body as json data 【发布时间】:2021-05-13 14:37:55 【问题描述】:

调用 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 并抛出为不将数据加载到服务器而设置的异常,并且在服务器日志中获得无法处理的身份,这意味着在发送请求时出现了一些错误。请看一下。

【问题讨论】:

【参考方案1】:

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

json.encode(autoModel.toJson())

【讨论】:

以上是关于使用 dart 创建发布请求,将信息作为 json 数据通过请求正文传递的主要内容,如果未能解决你的问题,请参考以下文章

在 Dart 中将类静态工厂作为方法参数传递

如何在 dart Parse Json 中使用类作为数据类型

如何在Dart / Flutter中使用Flask Server API将图像作为请求发布?

如何使用 swagger 使用 json 作为有效负载的 post 请求创建 api

Dart 将 HTML 转换为 JSON

Flutter - 网络请求与 json 解析