send method

  1. @override
Future<IOStreamedResponse> send(
  1. BaseRequest request
)
override

Sends an HTTP request and asynchronously returns the response.

Implementation

@override
Future<IOStreamedResponse> send(BaseRequest request) async {
  if (_inner == null) {
    throw ClientException(
        'HTTP request failed. Client is already closed.', request.url);
  }

  var stream = request.finalize();

  try {
    var ioRequest = (await _inner!.openUrl(request.method, request.url))
      ..followRedirects = request.followRedirects
      ..maxRedirects = request.maxRedirects
      ..contentLength = (request.contentLength ?? -1)
      ..persistentConnection = request.persistentConnection;
    request.headers.forEach((name, value) {
      ioRequest.headers.set(name, value);
    });

    var response = await stream.pipe(ioRequest) as HttpClientResponse;

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

    return IOStreamedResponse(
        response.handleError((Object error) {
          final httpException = error as HttpException;
          throw ClientException(httpException.message, httpException.uri);
        }, test: (error) => error is HttpException),
        response.statusCode,
        contentLength:
            response.contentLength == -1 ? null : response.contentLength,
        request: request,
        headers: headers,
        isRedirect: response.isRedirect,
        persistentConnection: response.persistentConnection,
        reasonPhrase: response.reasonPhrase,
        inner: response);
  } on SocketException catch (error) {
    throw _ClientSocketException(error, request.url);
  } on HttpException catch (error) {
    throw ClientException(error.message, error.uri);
  }
}