【问题标题】:How to set parameterised content type in put request in multipart request如何在多部分请求中的放置请求中设置参数化内容类型
【发布时间】:2021-05-16 08:02:45
【问题描述】:

我在使用 application/json 内容类型在表单数据中传递 requestDTO 对象时遇到问题。

我在下面的代码中使用了 Dio 库。但是在 Dio 参数中,值作为字符串传递。我从服务器收到了缺少 requestDTO 对象的错误。

Map<String, String> header = Map<String, String>();
header['Authorization'] = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI4NzY1NDMyMTA5IiwiYXV0aCI6IlJPTEVfQ09OVFJBQ1RPUiIsImV4cCI6MTYxNTYzNjIxMX0._pBVg6aWSl2K2bf416w1YnxYpQeEckL1Go9b_TRgaVruwcyh7tB5OdY9ujSiORn3rVpE39kPIX5zTTuwSJC3lQ';
header['content-type'] = 'multipart/form-data';


Map<String, dynamic> params = Map<String, dynamic>();
params['requestDTO'] = '{\n  "email": "c1@yopmail.com",\n  "firstName": "Contractor",\n  "fullName": "Contractor",\n  "imageUrl": "string",\n  "lastName": "two",\n  "phoneNumber": "8765432109",\n  "prAddressDTO": {\n    "addressLine": "string",\n    "city": "string",\n    "country": "string",\n    "id": 7009,\n    "latitude": 0,\n    "longitude": 0,\n    "prUserId": 6104,\n    "state": "string",\n    "zipcode": "string"\n  },\n  "prBusinessInfoDTO": {\n    "businessLicense": "string",\n    "businessName": "string",\n    "experience": 0,\n    "id": 6251,\n    "liabilityInsurance": "string",\n    "noOfWorkers": 0,\n    "notes": "string",\n    "prUserId": 6104,\n    "projectsCompleted": 0,\n    "servicesCategory": "string",\n    "servicesSubCategory": "string",\n    "stateRegistration": "string",\n    "taxIdentificationNumber": "string"\n  },\n  "userType": "CONTRACTOR"\n}';


// Create form data Request
FormData formData = new FormData.fromMap(params);

final option = Options(headers: header);
try {
  final response = await this.dio.put('http://180.211.99.164:9091/api/contractor/me', data: formData, options: option, onSendProgress: (sent, total) => Logger().v("uploadFile ${sent / total}"));
  this._handleNetworkSuccess(response: response);
} on DioError catch (error) {
  this._handleDioNetworkError(error: error);
}

【问题讨论】:

    标签: flutter dart dio


    【解决方案1】:

    请检查此答案https://stackoverflow.com/a/65737169/13439617

    它在没有DIO的情况下发送请求

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 2018-05-26
      • 2011-07-24
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      相关资源
      最近更新 更多