onResponse method

  1. @override
void onResponse(
  1. Response response,
  2. ResponseInterceptorHandler handler
)

Intercepts the response for an HTTP request made using Dio.

This method logs response details using the Infospect system after receiving a response.

  • response: The HTTP response.
  • handler: A response interceptor handler, which determines how the response should be handled.

Implementation

@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
  try {
    InfospectNetworkResponse httpResponse = InfospectNetworkResponse();

    dynamic body = '';
    int size = 0;
    if (response.data != null) {
      body = response.data;
      size = utf8.encode(response.data.toString()).length;
    }

    final Map<String, String> headers = {};
    response.headers.forEach((header, values) {
      headers[header] = values.toString();
    });

    infospect.addResponse(
        httpResponse.copyWith(
          status: response.statusCode,
          body: body,
          size: size,
          headers: headers,
        ),
        response.requestOptions.hashCode);
  } catch (e, st) {
    infospect.addLog(
      InfospectLog(
        message: e.toString(),
        stackTrace: st,
        error: e,
        level: DiagnosticLevel.error,
      ),
    );
  }
  handler.next(response);
}