httpRequest method

Future<String> httpRequest({
  1. required String certificateHash,
  2. required String method,
  3. required String url,
  4. Object? body,
  5. Map<String, String>? headers,
  6. String? encoding,
})

Sends an HTTP request with certificate pinning support.

This method performs an HTTP request using the specified method and url, with optional body, headers, and encoding. The request is secured using the provided certificateHash for certificate pinning.

On Android, the request is performed via FFI to native code. On iOS, it uses a MethodChannel to invoke native Swift code.

Throws an ArgumentError if body is not a String or Map. Throws an UnsupportedError if the platform is not Android or iOS.

Returns the parsed response as a String.

Parameters:

  • certificateHash: The hash of the certificate to pin the connection.
  • method: The HTTP method to use (e.g., 'GET', 'POST').
  • url: The URL to send the request to.
  • body: The request body, as a String or Map. Optional.
  • headers: The HTTP headers as a map of key-value pairs. Optional.
  • encoding: The encoding type for the request body (default is 'json'). Optional.

Implementation

Future<String> httpRequest({
  required String certificateHash,
  required String method,
  required String url,
  Object? body,
  Map<String, String>? headers,
  String? encoding,
}) async {
  final String bodyStr;
  if (body == null) {
    bodyStr = '';
  } else if (body is String) {
    bodyStr = body;
  } else if (body is Map) {
    bodyStr = jsonEncode(body);
  } else {
    throw ArgumentError('Body must be a String or Map');
  }

  final String headersStr = headers == null ? '' : jsonEncode(headers);

  if (Platform.isAndroid) {
    final certificateHashPtr = certificateHash.toNativeUtf8();
    final methodPtr = method.toNativeUtf8();
    final urlPtr = url.toNativeUtf8();
    final bodyPtr = bodyStr.toNativeUtf8();
    final headersPtr = headersStr.toNativeUtf8();
    final encodingPtr = (encoding ?? 'json').toNativeUtf8();
    ffi.Pointer<Utf8> resultPtr = ffi.Pointer.fromAddress(0);
    try {
      resultPtr = _httpRequestFfi!(
        certificateHashPtr,
        methodPtr,
        urlPtr,
        bodyPtr,
        headersPtr,
        encodingPtr,
      );
      if (resultPtr == ffi.Pointer.fromAddress(0)) {
        throw Exception("Error, null pointer returned from native code");
      }
      final result = resultPtr.toDartString();
      return parseResponse(result);
    } finally {
      malloc.free(methodPtr);
      malloc.free(urlPtr);
      malloc.free(bodyPtr);
      malloc.free(headersPtr);
      malloc.free(encodingPtr);
    }
  } else if (Platform.isIOS) {
    final response = await _channel.invokeMethod<String>('makeHttpRequest', {
      'certificateHash': certificateHash,
      'method': method,
      'url': url,
      'body': bodyStr,
      'headers': headersStr,
      'encoding': encoding ?? 'json',
    });
    return parseResponse(response!);
  } else {
    throw UnsupportedError(
      'Unsupported platform \'${Platform.operatingSystem}\'',
    );
  }
}