Flutter Dio网络请求:DioError [DioErrorType.RESPONSE]: Http status error [400]或者[500]

DioError [DioErrorType.RESPONSE]: Http status error [400]

1)检查请求参数是否正确 :

比如,如果参数map中value是一个数组,如下代码中的innerParticipateGroupsinnerParticipators的value值不能转成toString,否则就会报400

Map params = {
      "chamberId":resultId,
      "description":remark,
      "finishAt":1564979400000,
      "innerParticipateGroups":[],
      "innerParticipators":[],
      "name":meetingName,
      "reportId":"",
      "scheduleId":"",
      "startAt":1564975800000
    };

 2)如果以上正确,检查get和post调用时参数的就收方式

看下Dio(v2.1.0)的GET请求源码:

Future> get(
    String path, {
    Map queryParameters,
    Options options,
    CancelToken cancelToken,
    ProgressCallback onReceiveProgress,
  }) {
    return request(
      path,
      queryParameters: queryParameters,
      options: _checkOptions("GET", options),
      onReceiveProgress: onReceiveProgress,
      cancelToken: cancelToken,
    );
  }

GET请求的源码中参数接受要求传入的是queryParameters, 所以调用时用queryParameters

response = await dio.get(url, queryParameters: params);

POST源码:

/// Handy method to make http POST request, which is a alias of  [Dio.request].
  Future> post(
    String path, {
    data,
    Map queryParameters,
    Options options,
    CancelToken cancelToken,
    ProgressCallback onSendProgress,
    ProgressCallback onReceiveProgress,
  }) {
    return request(
      path,
      data: data,
      options: _checkOptions("POST", options),
      queryParameters: queryParameters,
      cancelToken: cancelToken,
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,
    );
  }

POST源码中参数接受有dataqueryParameters,调用时key用data,用queryParameters会报400,queryParameters这个key不知道具体怎么用,官方也没具体说明,官方文档中post请求都是用data传参,有懂的大神麻烦给补充一下。

response = await dio.post(url, data : params);

 DioError [DioErrorType.RESPONSE]: Http status error [500]

检查header是否设置了或者设置错了

HttpGo() {
    dio = Dio(BaseOptions(
      baseUrl: 'https://www.***.com/',
      headers: getHeaders(),
      connectTimeout: 5000,
      receiveTimeout: 3000,
    ));
  }
  getHeaders () {
    return {
      'Accept':'application/json, text/plain, */*',
      'Content-Type':'application/json',
      'Authorization':"**",
      'User-Aagent':"4.1.0;android;6.0.1;default;A001",
      "HZUID":"2",
    };
  }

 DioError [DioErrorType.RESPONSE]: Http status error [415]

把请求参数数据格式FormData换成Map试试

如果你觉得对你有用,就点个赞支持一下吧~ 谢谢啦~

 

你可能感兴趣的:(Flutter)