【问题标题】:Flutter Dio response type<!DOCTYPE html> not jsonFlutter Dio 响应类型<!DOCTYPE html> 不是 json
【发布时间】:2022-07-06 15:05:20
【问题描述】:

我使用 dio.post 作为付款方式 API 在 Postman 上运行良好,但收到响应时不是 JSON 类型 然后应用程序无法处理对可用数据的响应

主要问题是支付成功但是给用户一个错误this is the response.data

我的 Dio 初始化为

  static init() {
  _dio = Dio(
    BaseOptions(
      baseUrl: baseUrl,
      receiveDataWhenStatusError: true,
      followRedirects: false,
      validateStatus: (status) {
      return status! < 500;
      },
    connectTimeout: 60 * 1000 ,
    // 60 seconds
    receiveTimeout: 60 * 1000 ,
    ),
  );
}

我的方法是发布

static Future<Response> postData({
required String endPoint,
required dynamic data,
String? token,
 }) async {
// set headers here
_dio.options.headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer $token',
};
return await _dio.post(endPoint, data: data);
}

我这样发送请求

String _fileName = counterImage.path.split('/').last;
        var _image = await MultipartFile.fromFile(
          counterImage.path,
          filename: _fileName,
          contentType: MediaType('image', 'png'),
        );
        FormData _data = FormData.fromMap({
          'my_wallet': mainWallet.id,
          'amount': amount,
          'product': product.id,
          'units_count': unitsCount,
          'image': _image,
        });

        await DioHelper.postData(
          endPoint: '$paymentEndPoint/$subWalletID',
          token: payToken,
          data: _data,
        ).then((value) {
          if (kDebugMode) {
            print('Pay done response ${value.data}');
          }
       

【问题讨论】:

    标签: flutter api postman dio


    【解决方案1】:

    在 post Method (content-type) 中试试这个:

    static Future<Response> postData({
    required String endPoint,
    required dynamic data,
    String? token,
     }) async {
    // set headers here
    _dio.options.headers = {
      'Content-Type': 'application/json;charset=UTF-8',
      'Authorization': 'Bearer $token',
    };
    return await _dio.post(endPoint, data: data);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-06
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 2011-06-11
      • 1970-01-01
      • 2015-01-08
      • 2021-06-29
      相关资源
      最近更新 更多