Index: pkg/http/lib/src/base_client.dart |
diff --git a/pkg/http/lib/src/base_client.dart b/pkg/http/lib/src/base_client.dart |
index 8c252654741f09d110f55a8648700e477d06f7d0..86925c36fed345923a91a6cd2c4803aceb7a5012 100644 |
--- a/pkg/http/lib/src/base_client.dart |
+++ b/pkg/http/lib/src/base_client.dart |
@@ -5,6 +5,7 @@ |
library base_client; |
import 'dart:async'; |
+import 'dart:convert'; |
import 'dart:io'; |
import 'dart:typed_data'; |
@@ -33,27 +34,49 @@ abstract class BaseClient implements Client { |
Future<Response> get(url, {Map<String, String> headers}) => |
_sendUnstreamed("GET", url, headers); |
- /// Sends an HTTP POST request with the given headers and fields to the given |
- /// URL, which can be a [Uri] or a [String]. If any fields are specified, the |
- /// content-type is automatically set to |
- /// `"application/x-www-form-urlencoded"`. |
+ /// Sends an HTTP POST 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 UTF-8. |
/// |
/// For more fine-grained control over the request, use [send] instead. |
- Future<Response> post(url, |
- {Map<String, String> headers, |
- Map<String, String> fields}) => |
- _sendUnstreamed("POST", url, headers, fields); |
+ Future<Response> post(url, {Map<String, String> headers, body, |
+ Encoding encoding}) => |
+ _sendUnstreamed("POST", url, headers, body, encoding); |
- /// Sends an HTTP PUT request with the given headers and fields to the given |
- /// URL, which can be a [Uri] or a [String]. If any fields are specified, the |
- /// content-type is automatically set to |
- /// `"application/x-www-form-urlencoded"`. |
+ /// 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". |
+ /// |
+ /// 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 UTF-8. |
/// |
/// For more fine-grained control over the request, use [send] instead. |
- Future<Response> put(url, |
- {Map<String, String> headers, |
- Map<String, String> fields}) => |
- _sendUnstreamed("PUT", url, headers, fields); |
+ Future<Response> put(url, {Map<String, String> headers, body, |
+ Encoding encoding}) => |
+ _sendUnstreamed("PUT", url, headers, body, encoding); |
/// Sends an HTTP DELETE request with the given headers to the given URL, |
/// which can be a [Uri] or a [String]. |
@@ -103,17 +126,25 @@ abstract class BaseClient implements Client { |
Future<StreamedResponse> send(BaseRequest request); |
/// Sends a non-streaming [Request] and returns a non-streaming [Response]. |
- Future<Response> _sendUnstreamed( |
- String method, url, Map<String, String> headers, |
- [Map<String, String> fields]) { |
- // Wrap everything in a Future block so that synchronous validation errors |
- // are passed asynchronously through the Future chain. |
- return async.then((_) { |
+ Future<Response> _sendUnstreamed(String method, url, |
+ Map<String, String> headers, [body, Encoding encoding]) { |
+ return new Future.sync(() { |
if (url is String) url = Uri.parse(url); |
var request = new Request(method, url); |
if (headers != null) request.headers.addAll(headers); |
- if (fields != null && !fields.isEmpty) request.bodyFields = fields; |
+ if (encoding != null) request.encoding = encoding; |
+ if (body != null) { |
+ if (body is String) { |
+ request.body = body; |
+ } else if (body is List) { |
+ request.bodyBytes = body; |
+ } else if (body is Map) { |
+ request.bodyFields = body; |
+ } else { |
+ throw new ArgumentError('Invalid request body "$body".'); |
+ } |
+ } |
return send(request); |
}).then(Response.fromStream); |