openHttp method

Future<void> openHttp({
  1. String method = 'GET',
  2. required Uri uri,
  3. ContentType? contentType,
})

Loads content using HTTP client.

Example

import 'package:universal_html/parsing.dart';

Future<void> main() async {
  final controller = WindowController();
  await controller.openHttp(
    method: 'GET',
    uri: Uri.parse('https://www.ietf.org/'),
  );
  final document = controller.window.document;
  // ...
}

Implementation

Future<void> openHttp({
  String method = 'GET',
  required Uri uri,
  ContentType? contentType,
}) async {
  if (isTopLevelWindowInsideBrowser) {
    throw StateError('Failed to mutate the main window inside a browser');
  }

  // Write HTTP request.
  final client = io.HttpClient();
  final request = await client.openUrl(method, uri);
  if (contentType != null) {
    request.headers.contentType = contentType;
  }

  // Read HTTP response.
  final response = await request.close();
  final content = await utf8.decodeStream(response);

  return openContent(content);
}