| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// A composable, [Future]-based library for making HTTP requests. |
| 6 /// |
| 7 /// The easiest way to use this library is via the top-level functions. They |
| 8 /// allow you to make individual HTTP requests with minimal hassle: |
| 9 /// |
| 10 /// import 'package:http/http.dart' as http; |
| 11 /// |
| 12 /// var url = "http://example.com/whatsit/create"; |
| 13 /// http.post(url, fields: {"name": "doodle", "color": "blue"}) |
| 14 /// .then((response) { |
| 15 /// print("Response status: ${response.statusCode}"); |
| 16 /// print("Response body: ${response.body}"); |
| 17 /// }); |
| 18 /// |
| 19 /// http.read("http://example.com/foobar.txt").then(print); |
| 20 /// |
| 21 /// If you're making multiple requests to the same server, you can keep open a |
| 22 /// persistent connection by using a [Client] rather than making one-off |
| 23 /// requests. If you do this, make sure to close the client when you're done: |
| 24 /// |
| 25 /// var client = new http.Client(); |
| 26 /// client.post( |
| 27 /// "http://example.com/whatsit/create", |
| 28 /// fields: {"name": "doodle", "color": "blue"}) |
| 29 /// .chain((response) => client.get(response.bodyFields['uri'])) |
| 30 /// .transform((response) => print(response.body)) |
| 31 /// .onComplete((_) => client.close()); |
| 32 /// |
| 33 /// You can also exert more fine-grained control over your requests and |
| 34 /// responses by creating [Request] or [StreamedRequest] objects yourself and |
| 35 /// passing them to [Client.send]. |
| 36 /// |
| 37 /// This package is designed to be composable. This makes it easy for external |
| 38 /// libraries to work with one another to add behavior to it. Libraries wishing |
| 39 /// to add behavior should create a subclass of [BaseClient] that wraps another |
| 40 /// [BaseClient] and adds the desired behavior: |
| 41 /// |
| 42 /// class UserAgentClient extends http.BaseClient { |
| 43 /// final String userAgent; |
| 44 /// final HttpClient _inner; |
| 45 /// |
| 46 /// UserAgentClient(this.userAgent, this._inner); |
| 47 /// |
| 48 /// Future<StreamedResponse> send(BaseRequest request) { |
| 49 /// request.headers[HttpHeaders.USER_AGENT] = userAgent; |
| 50 /// return _inner.send(request); |
| 51 /// } |
| 52 /// } |
| 53 /// |
| 54 /// In turn, libraries using [Client] should take a [BaseClient] so that the |
| 55 /// decorated clients can be used transparently. |
| 56 |
| 57 library http; |
| 58 |
| 59 import 'dart:scalarlist'; |
| 60 import 'dart:uri'; |
| 61 |
| 62 import 'src/client.dart'; |
| 63 import 'src/response.dart'; |
| 64 |
| 65 export 'src/base_client.dart'; |
| 66 export 'src/base_request.dart'; |
| 67 export 'src/base_response.dart'; |
| 68 export 'src/client.dart'; |
| 69 export 'src/request.dart'; |
| 70 export 'src/response.dart'; |
| 71 export 'src/streamed_request.dart'; |
| 72 export 'src/streamed_response.dart'; |
| 73 |
| 74 /// Sends an HTTP HEAD request with the given headers to the given URL, which |
| 75 /// can be a [Uri] or a [String]. |
| 76 /// |
| 77 /// This automatically initializes a new [Client] and closes that client once |
| 78 /// the request is complete. If you're planning on making multiple requests to |
| 79 /// the same server, you should use a single [Client] for all of those requests. |
| 80 /// |
| 81 /// For more fine-grained control over the request, use [Request] instead. |
| 82 Future<Response> head(url, {Map<String, String> headers}) => |
| 83 _withClient((client) => client.head(url, headers: headers)); |
| 84 |
| 85 /// Sends an HTTP GET request with the given headers to the given URL, which can |
| 86 /// be a [Uri] or a [String]. |
| 87 /// |
| 88 /// This automatically initializes a new [Client] and closes that client once |
| 89 /// the request is complete. If you're planning on making multiple requests to |
| 90 /// the same server, you should use a single [Client] for all of those requests. |
| 91 /// |
| 92 /// For more fine-grained control over the request, use [Request] instead. |
| 93 Future<Response> get(url, {Map<String, String> headers}) => |
| 94 _withClient((client) => client.get(url, headers: headers)); |
| 95 |
| 96 /// Sends an HTTP POST request with the given headers and fields to the given |
| 97 /// URL, which an be a [Uri] or a [String]. If any fields are specified, the |
| 98 /// content-type is automatically set to `"application/x-www-form-urlencoded"`. |
| 99 /// |
| 100 /// This automatically initializes a new [Client] and closes that client once |
| 101 /// the request is complete. If you're planning on making multiple requests to |
| 102 /// the same server, you should use a single [Client] for all of those requests. |
| 103 /// |
| 104 /// For more fine-grained control over the request, use [Request] or |
| 105 /// [StreamedRequest] instead. |
| 106 Future<Response> post(url, |
| 107 {Map<String, String> headers, |
| 108 Map<String, String> fields}) => |
| 109 _withClient((client) => client.post(url, headers: headers, fields: fields)); |
| 110 |
| 111 /// Sends an HTTP POST request with the given headers and fields to the given |
| 112 /// URL, which can be a [Uri] or a [String]. If any fields are specified, the |
| 113 /// content-type is automatically set to `"application/x-www-form-urlencoded"`. |
| 114 /// |
| 115 /// This automatically initializes a new [Client] and closes that client once |
| 116 /// the request is complete. If you're planning on making multiple requests to |
| 117 /// the same server, you should use a single [Client] for all of those requests. |
| 118 /// |
| 119 /// For more fine-grained control over the request, use [Request] or |
| 120 /// [StreamedRequest] instead. |
| 121 Future<Response> put(url, |
| 122 {Map<String, String> headers, |
| 123 Map<String, String> fields}) => |
| 124 _withClient((client) => client.put(url, headers: headers, fields: fields)); |
| 125 |
| 126 /// Sends an HTTP DELETE request with the given headers to the given URL, which |
| 127 /// can be a [Uri] or a [String]. |
| 128 /// |
| 129 /// This automatically initializes a new [Client] and closes that client once |
| 130 /// the request is complete. If you're planning on making multiple requests to |
| 131 /// the same server, you should use a single [Client] for all of those requests. |
| 132 /// |
| 133 /// For more fine-grained control over the request, use [Request] instead. |
| 134 Future<Response> delete(url, {Map<String, String> headers}) => |
| 135 _withClient((client) => client.delete(url, headers: headers)); |
| 136 |
| 137 /// Sends an HTTP GET request with the given headers to the given URL, which can |
| 138 /// be a [Uri] or a [String], and returns a Future that completes to the body of |
| 139 /// the response as a [String]. |
| 140 /// |
| 141 /// The Future will emit an [HttpException] if the response doesn't have a |
| 142 /// success status code. |
| 143 /// |
| 144 /// This automatically initializes a new [Client] and closes that client once |
| 145 /// the request is complete. If you're planning on making multiple requests to |
| 146 /// the same server, you should use a single [Client] for all of those requests. |
| 147 /// |
| 148 /// For more fine-grained control over the request and response, use [Request] |
| 149 /// instead. |
| 150 Future<String> read(url, {Map<String, String> headers}) => |
| 151 _withClient((client) => client.read(url, headers: headers)); |
| 152 |
| 153 /// Sends an HTTP GET request with the given headers to the given URL, which can |
| 154 /// be a [Uri] or a [String], and returns a Future that completes to the body of |
| 155 /// the response as a list of bytes. |
| 156 /// |
| 157 /// The Future will emit an [HttpException] if the response doesn't have a |
| 158 /// success status code. |
| 159 /// |
| 160 /// This automatically initializes a new [Client] and closes that client once |
| 161 /// the request is complete. If you're planning on making multiple requests to |
| 162 /// the same server, you should use a single [Client] for all of those requests. |
| 163 /// |
| 164 /// For more fine-grained control over the request and response, use [Request] |
| 165 /// instead. |
| 166 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => |
| 167 _withClient((client) => client.readBytes(url, headers: headers)); |
| 168 |
| 169 Future _withClient(Future fn(Client)) { |
| 170 var client = new Client(); |
| 171 var future = fn(client); |
| 172 future.onComplete((_) => client.close()); |
| 173 return future; |
| 174 } |
| OLD | NEW |