http.dart 7.16 KB
Newer Older
1 2 3 4
// Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

5
/// A composable, [Future]-based library for making HTTP requests.
6 7 8 9
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

10
import 'client.dart';
11 12
import 'response.dart';

13 14 15 16 17 18 19 20 21 22 23 24 25 26
export 'base_client.dart';
export 'base_request.dart';
export 'base_response.dart';
export 'byte_stream.dart';
export 'client.dart';
export 'exception.dart';
export 'io_client.dart';
export 'multipart_file.dart';
export 'multipart_request.dart';
export 'request.dart';
export 'response.dart';
export 'streamed_request.dart';
export 'streamed_response.dart';

27 28 29
/// Sends an HTTP HEAD request with the given headers to the given URL, which
/// can be a [Uri] or a [String].
///
30
/// This automatically initializes a new [Client] and closes that client once
31
/// the request is complete. If you're planning on making multiple requests to
32 33 34 35 36
/// the same server, you should use a single [Client] for all of those requests.
///
/// For more fine-grained control over the request, use [Request] instead.
Future<Response> head(dynamic url, {Map<String, String> headers}) =>
  _withClient((Client client) => client.head(url, headers: headers));
37 38 39 40

/// Sends an HTTP GET request with the given headers to the given URL, which can
/// be a [Uri] or a [String].
///
41
/// This automatically initializes a new [Client] and closes that client once
42
/// the request is complete. If you're planning on making multiple requests to
43 44 45 46 47
/// the same server, you should use a single [Client] for all of those requests.
///
/// For more fine-grained control over the request, use [Request] instead.
Future<Response> get(dynamic url, {Map<String, String> headers}) =>
  _withClient((Client client) => client.get(url, headers: headers));
48 49 50 51

/// Sends an HTTP POST request with the given headers and body to the given URL,
/// which can be a [Uri] or a [String].
///
52 53 54 55 56 57 58 59 60 61 62 63 64 65
/// [body] sets the body of the request. It can be a [String], a [List<int>] or
/// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and
/// used as the body of the request. The content-type of the request will
/// default to "text/plain".
///
/// If [body] is a List, it's used as a list of bytes for the body of the
/// request.
///
/// If [body] is a Map, it's encoded as form fields using [encoding]. The
/// content-type of the request will be set to
/// `"application/x-www-form-urlencoded"`; this cannot be overridden.
///
/// [encoding] defaults to [UTF8].
///
66 67 68 69 70 71
/// For more fine-grained control over the request, use [Request] or
/// [StreamedRequest] instead.
Future<Response> post(dynamic url, {Map<String, String> headers, dynamic body,
    Encoding encoding}) =>
  _withClient((Client client) => client.post(url,
      headers: headers, body: body, encoding: encoding));
72 73 74 75 76 77 78 79

/// Sends an HTTP PUT request with the given headers and body to the given URL,
/// which can be a [Uri] or a [String].
///
/// [body] sets the body of the request. It can be a [String], a [List<int>] or
/// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and
/// used as the body of the request. The content-type of the request will
/// default to "text/plain".
80 81 82 83 84 85 86 87 88 89
///
/// If [body] is a List, it's used as a list of bytes for the body of the
/// request.
///
/// If [body] is a Map, it's encoded as form fields using [encoding]. The
/// content-type of the request will be set to
/// `"application/x-www-form-urlencoded"`; this cannot be overridden.
///
/// [encoding] defaults to [UTF8].
///
90 91 92 93 94 95
/// For more fine-grained control over the request, use [Request] or
/// [StreamedRequest] instead.
Future<Response> put(dynamic url, {Map<String, String> headers, dynamic body,
    Encoding encoding}) =>
  _withClient((Client client) => client.put(url,
      headers: headers, body: body, encoding: encoding));
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

/// Sends an HTTP PATCH request with the given headers and body to the given
/// URL, which can be a [Uri] or a [String].
///
/// [body] sets the body of the request. It can be a [String], a [List<int>] or
/// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and
/// used as the body of the request. The content-type of the request will
/// default to "text/plain".
///
/// If [body] is a List, it's used as a list of bytes for the body of the
/// request.
///
/// If [body] is a Map, it's encoded as form fields using [encoding]. The
/// content-type of the request will be set to
/// `"application/x-www-form-urlencoded"`; this cannot be overridden.
///
/// [encoding] defaults to [UTF8].
///
114 115 116 117 118 119
/// For more fine-grained control over the request, use [Request] or
/// [StreamedRequest] instead.
Future<Response> patch(dynamic url, {Map<String, String> headers, dynamic body,
    Encoding encoding}) =>
  _withClient((Client client) => client.patch(url,
      headers: headers, body: body, encoding: encoding));
120 121 122 123

/// Sends an HTTP DELETE request with the given headers to the given URL, which
/// can be a [Uri] or a [String].
///
124
/// This automatically initializes a new [Client] and closes that client once
125
/// the request is complete. If you're planning on making multiple requests to
126 127 128 129 130
/// the same server, you should use a single [Client] for all of those requests.
///
/// For more fine-grained control over the request, use [Request] instead.
Future<Response> delete(dynamic url, {Map<String, String> headers}) =>
  _withClient((Client client) => client.delete(url, headers: headers));
131 132 133 134 135

/// Sends an HTTP GET request with the given headers to the given URL, which can
/// be a [Uri] or a [String], and returns a Future that completes to the body of
/// the response as a [String].
///
136 137
/// The Future will emit a [ClientException] if the response doesn't have a
/// success status code.
138
///
139
/// This automatically initializes a new [Client] and closes that client once
140
/// the request is complete. If you're planning on making multiple requests to
141 142 143 144 145 146
/// the same server, you should use a single [Client] for all of those requests.
///
/// For more fine-grained control over the request and response, use [Request]
/// instead.
Future<String> read(dynamic url, {Map<String, String> headers}) =>
  _withClient((Client client) => client.read(url, headers: headers));
147 148 149 150 151

/// Sends an HTTP GET request with the given headers to the given URL, which can
/// be a [Uri] or a [String], and returns a Future that completes to the body of
/// the response as a list of bytes.
///
152 153
/// The Future will emit a [ClientException] if the response doesn't have a
/// success status code.
154
///
155
/// This automatically initializes a new [Client] and closes that client once
156
/// the request is complete. If you're planning on making multiple requests to
157
/// the same server, you should use a single [Client] for all of those requests.
Adam Barth's avatar
Adam Barth committed
158
///
159 160 161 162
/// For more fine-grained control over the request and response, use [Request]
/// instead.
Future<Uint8List> readBytes(dynamic url, {Map<String, String> headers}) =>
  _withClient((Client client) => client.readBytes(url, headers: headers));
Adam Barth's avatar
Adam Barth committed
163

164 165 166 167 168 169 170
Future/*<T>*/ _withClient/*<T>*/(Future/*<T>*/ fn(Client client)) async {
  Client client = new Client();
  try {
    return await fn(client);
  } finally {
    client.close();
  }
171
}