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() {
  var clientFactory = Client.new; // The default Client.
  if (Platform.isIOS || Platform.isMacOS) {
    clientFactory = CupertinoClient.defaultSessionConfiguration.call;
  }
  runWithClient(() => runApp(const MyFlutterApp()), clientFactory);
}

After the above setup, calling Client methods or any of the package:http convenient functions (e.g. get) will result in CupertinoClient being used on macOS and iOS.

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.
Data
A container for byte data.
HTTPURLResponse
The response associated with loading a HTTP URL.
MutableData
A container for byte data.
MutableURLRequest
A mutable request to load a URL.
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.

Enums

HTTPCookieAcceptPolicy
Settings for controlling whether cookies will be accepted.
URLRequestCachePolicy
Controls how response data is cached.
URLRequestNetworkService
Provides in indication to the operating system on what type of requests are being sent.
URLSessionMultipathServiceType
URLSessionResponseDisposition
Controls how URLSessionTask execute will proceed after the response is received.
URLSessionTaskState
The possible states of a URLSessionTask.
URLSessionWebSocketMessageType
The type of a WebSocket message i.e. text or data.

Exceptions / Errors

ConnectionException
An error occurred while connecting to the peer.
Error
Information about a failure.