openUrl method

  1. @override
Future<HttpClientRequest> openUrl(
  1. String method,
  2. Uri url
)
override

Opens an HTTP request by method and URI.

Intercepts the request to attach a unique ID and to track the request body, forwarding request details to the native side using MethodChannelController.

Implementation

@override
Future<HttpClientRequest> openUrl(String method, Uri url) {
  return client.openUrl(method, url).then((request) {
    final wrapped = _wrapResponse(request);
    final body = <int>[]; // Buffer to capture the request body bytes

    // For non-body methods (GET, HEAD, DELETE, etc.), notify immediately
    if (method.toUpperCase() != 'POST' && method.toUpperCase() != 'PUT') {
      scheduleMicrotask(() {
        MethodChannelController.requestWillBeSent(
          StethoInspectorRequest(
            url: request.uri.toString(),
            headers: headersToMap(request.headers),
            method: request.method,
            id: wrapped.id,
            body: body,
            friendlyNameExtra: null,
          ),
        );
      });
    } else {
      // For POST/PUT, listen to the request stream to capture the body progressively
      wrapped.stream.listen((chunk) {
        body.addAll(chunk);
        scheduleMicrotask(() {
          debugPrint("requestWillBeSent - ${request.uri}");
          MethodChannelController.requestWillBeSent(
            StethoInspectorRequest(
              url: request.uri.toString(),
              headers: headersToMap(request.headers),
              method: request.method,
              id: wrapped.id,
              body: body,
            ),
          );
        });
      });
    }

    return wrapped;
  });
}