cupertino_http library

Provides access to the Foundation URL Loading System.

NOTE: If sandboxed with the App Sandbox (the default Flutter configuration on macOS) then the com.apple.security.network.client entitlement is required to use package:cupertino_http. See Entitlements and the App Sandbox.

CupertinoClient

The most convenient way to package:cupertino_http it is through CupertinoClient.

import 'package:cupertino_http/cupertino_http.dart';

void main() async {
  var client = CupertinoClient.defaultSessionConfiguration();
  final response = await client.get(
      Uri.https('www.googleapis.com', '/books/v1/volumes', {'q': '{http}'}));
  if (response.statusCode != 200) {
    throw HttpException('bad response: ${response.statusCode}');
  }

  final decodedResponse =
      jsonDecode(utf8.decode(response.bodyBytes)) as Map;

  final itemCount = decodedResponse['totalItems'];
  print('Number of books about http: $itemCount.');
  for (var i = 0; i < min(itemCount, 10); ++i) {
    print(decodedResponse['items'][i]['volumeInfo']['title']);
  }
}

CupertinoClient is an implementation of the package:http Client, which means that it can easily used conditionally based on the current platform.

void main() {
  final Client httpClient;
  if (Platform.isIOS || Platform.isMacOS) {
    final config = URLSessionConfiguration.ephemeralSessionConfiguration()
      ..cache = URLCache.withCapacity(memoryCapacity: 2 * 1024 * 1024)
      ..httpAdditionalHeaders = {'User-Agent': 'Book Agent'};
    httpClient = CupertinoClient.fromSessionConfiguration(config);
  } else {
    httpClient = IOClient(HttpClient()..userAgent = 'Book Agent');
  }

  runApp(Provider<Client>(
      create: (_) => httpClient,
      child: const BookSearchApp(),
      dispose: (_, client) => client.close()));
 }

NSURLSession API

package:cupertino_http also allows direct access to the Foundation URL Loading System APIs.

void main() {
  final url = Uri.https('www.example.com', '/');
  final session = URLSession.sharedSession();
  final task = session.dataTaskWithCompletionHandler(
    URLRequest.fromUrl(url),
      (data, response, error) {
    if (error == null) {
      if (response != null && response.statusCode == 200) {
        print(response);  // Do something with the response.
        return;
      }
    }
    print(error);  // Handle errors.
  });
  task.resume();
}

Classes

CupertinoClient
A HTTP Client based on the Foundation URL Loading System.
CupertinoWebSocket
A WebSocket implemented using the NSURLSessionWebSocketTask API.
HTTPURLResponse
The response associated with loading a HTTP URL.
MutableURLRequest
A mutable request to load a URL.
NSURLSessionWebSocketCloseCode
The WebSocket close codes follow the close codes given in the RFC
URLCache
A cache for URLRequests. Used by URLSessionConfiguration.cache.
URLRequest
A request to load a URL.
URLResponse
The response associated with loading an URL.
URLSession
A client that can make network requests to a server.
URLSessionConfiguration
Controls the behavior of a URLSession.
URLSessionDownloadTask
A task associated with downloading a URI to a file.
URLSessionTask
A task associated with downloading a URI.
URLSessionWebSocketMessage
A WebSocket message.
URLSessionWebSocketTask
A task associated with a WebSocket connection.

Exceptions / Errors

ConnectionException
An error occurred while connecting to the peer.