send<T> method

  1. @override
Future<Response<T>> send<T>(
  1. Request<T> request
)
override

Sends an HTTP Request.

Implementation

@override
Future<Response<T>> send<T>(Request<T> request) async {
  var stream = request.bodyBytes.asBroadcastStream();
  io.HttpClientRequest? ioRequest;
  try {
    _httpClient!.connectionTimeout = timeout;
    ioRequest = (await _httpClient!.openUrl(request.method, request.url))
      ..followRedirects = request.followRedirects
      ..persistentConnection = request.persistentConnection
      ..maxRedirects = request.maxRedirects
      ..contentLength = request.contentLength ?? -1;
    request.headers.forEach(ioRequest.headers.set);

    var response = timeout == null
        ? await stream.pipe(ioRequest) as io.HttpClientResponse
        : await stream.pipe(ioRequest).timeout(timeout!)
            as io.HttpClientResponse;

    var headers = <String, String>{};
    response.headers.forEach((key, values) {
      headers[key] = values.join(',');
    });

    final bodyBytes = (response);

    final interceptionResponse = await request.responseInterceptor
        ?.call(request, T, IOHttpResponse(response: response));
    if (interceptionResponse != null) return interceptionResponse;

    final stringBody = await bodyBytesToString(bodyBytes, headers);

    final body = bodyDecoded<T>(
      request,
      stringBody,
      response.headers.contentType?.mimeType,
    );

    return Response(
      headers: headers,
      request: request,
      statusCode: response.statusCode,
      statusText: response.reasonPhrase,
      bodyBytes: bodyBytes,
      body: body,
      bodyString: stringBody,
    );
  } on TimeoutException catch (_) {
    ioRequest?.abort();
    rethrow;
  } on io.HttpException catch (error) {
    throw GetHttpException(error.message, error.uri);
  }
}