Connection closed before full header was received http post in flutter

840

I have came with this work around by creating this interceptor.

The idea of it is when encountering this random error just to retry the request.

/// Interceptor
class RetryOnConnectionChangeInterceptor extends Interceptor {
  final Dio dio;

  RetryOnConnectionChangeInterceptor({
    required this.dio,
  });

  @override
  void onError(DioError err, ErrorInterceptorHandler handler) async {
  if (_shouldRetryOnHttpException(err)) {
      try {
        handler.resolve(await DioHttpRequestRetrier(dio: dio).requestRetry(err.requestOptions).catchError((e) {
          handler.next(err);
        }));
      } catch (e) {
        handler.next(err);
      }
    } else {
      handler.next(err);
    }

  }

  bool _shouldRetryOnHttpException(DioError err) {
    return err.type == DioErrorType.other &&
        ((err.error is HttpException && err.message.contains('Connection closed before full header was received')));
  }
}

/// Retrier
class DioHttpRequestRetrier {
  final Dio dio;

  DioHttpRequestRetrier({
    required this.dio,
  });

  Future<Response> requestRetry(RequestOptions requestOptions) async {
    return dio.request(
      requestOptions.path,
      cancelToken: requestOptions.cancelToken,
      data: requestOptions.data,
      onReceiveProgress: requestOptions.onReceiveProgress,
      onSendProgress: requestOptions.onSendProgress,
      queryParameters: requestOptions.queryParameters,
      options: Options(
        contentType: requestOptions.contentType,
        headers: requestOptions.headers,
        sendTimeout: requestOptions.sendTimeout,
        receiveTimeout: requestOptions.receiveTimeout,
        extra: requestOptions.extra,
        followRedirects: requestOptions.followRedirects,
        listFormat: requestOptions.listFormat,
        maxRedirects: requestOptions.maxRedirects,
        method: requestOptions.method,
        receiveDataWhenStatusError: requestOptions.receiveDataWhenStatusError,
        requestEncoder: requestOptions.requestEncoder,
        responseDecoder: requestOptions.responseDecoder,
        responseType: requestOptions.responseType,
        validateStatus: requestOptions.validateStatus,
      ),
    );
  }
}

Usage: add this interceptor [RetryOnConnectionChangeInterceptor] to your Dio client instance

Share:
840
Deybi Tabora Paz
Author by

Deybi Tabora Paz

Updated on November 26, 2022

Comments

  • Deybi Tabora Paz
    Deybi Tabora Paz over 1 year

    I'm having problems trying to consume my API, and after following up on this problem I'm stuck in the matter and I've tried different emulators in various versions and the problem persists.

    Error:

    DioError [DioErrorType.other]: HttpException: Connection closed before full header was received, uri = http://10.0.2.2:7108/Users/authenticate
    

    Flutter doctor

    enter image description here

    Http Post

    class AuthenticateRemoteApi extends AuthenticateGateway {
      final AuthenticateMapper _authenticateMapper = AuthenticateMapper();
    
      @override
      Future<SesionUser> login(Authenticate user) async {
        var dio = Dio();
        dio.options.headers['content-Type'] = 'application/json';
        String url = 'http://10.0.2.2:7108/Users/authenticate';
    
        try {
          Response response = await dio.post(url, data: authenticateModelToJson(user));
          return _authenticateMapper.fromMap(jsonDecode(response.data));
        } catch (e) {
          throw Exception(e);
        }
      }
    }
    
    • Eduardo Yamauchi
      Eduardo Yamauchi about 2 years
      Could you share more information about the code? I think you're closing your connection without wait the response.
    • Deybi Tabora Paz
      Deybi Tabora Paz about 2 years
      Yes, I already edited my question. @EduardoYamauchi
    • fsbelinda
      fsbelinda about 2 years
      Test with Postman or another tool first just to be sure the Api is ok and then try to use another package like http to see if it works. If not then there should be a mistake with the request in your code.
    • Deybi Tabora Paz
      Deybi Tabora Paz about 2 years
      The api is fine I can consume it from postman without problems @BélindaG.Freitas