OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of $LIBRARYNAME; | 5 part of $LIBRARYNAME; |
6 | 6 |
7 /** | 7 /** |
8 * A utility for retrieving data from a URL. | 8 * A utility for retrieving data from a URL. |
9 * | 9 * |
10 * HttpRequest can be used to obtain data from http, ftp, and file | 10 * HttpRequest can be used to obtain data from http, ftp, and file |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 * | 45 * |
46 * * [request] | 46 * * [request] |
47 */ | 47 */ |
48 static Future<String> getString(String url, | 48 static Future<String> getString(String url, |
49 {bool withCredentials, void onProgress(ProgressEvent e)}) { | 49 {bool withCredentials, void onProgress(ProgressEvent e)}) { |
50 return request(url, withCredentials: withCredentials, | 50 return request(url, withCredentials: withCredentials, |
51 onProgress: onProgress).then((xhr) => xhr.responseText); | 51 onProgress: onProgress).then((xhr) => xhr.responseText); |
52 } | 52 } |
53 | 53 |
54 /** | 54 /** |
| 55 * Makes a server POST request with the specified data encoded as form data. |
| 56 * |
| 57 * This is similar to sending a FormData object with broader browser |
| 58 * support but limited to string values. |
| 59 * |
| 60 * See also: |
| 61 * |
| 62 * * [request] |
| 63 */ |
| 64 static Future<HttpRequest> postFormData(String url, Map<String, String> data, |
| 65 {bool withCredentials, String responseType, |
| 66 Map<String, String> requestHeaders, |
| 67 void onProgress(ProgressEvent e)}) { |
| 68 |
| 69 var parts = []; |
| 70 data.forEach((key, value) { |
| 71 parts.add('${Uri.encodeQueryComponent(key)}=' |
| 72 '${Uri.encodeQueryComponent(value)}'); |
| 73 }); |
| 74 var formData = parts.join('&'); |
| 75 |
| 76 if (requestHeaders == null) { |
| 77 requestHeaders = <String, String>{}; |
| 78 } |
| 79 requestHeaders.putIfAbsent('Content-Type', |
| 80 () => 'application/x-www-form-urlencoded; charset=UTF-8'); |
| 81 |
| 82 return request(url, method: 'POST', withCredentials: withCredentials, |
| 83 responseType: responseType, |
| 84 requestHeaders: requestHeaders, sendData: formData, |
| 85 onProgress: onProgress); |
| 86 } |
| 87 |
| 88 /** |
55 * Creates a URL request for the specified [url]. | 89 * Creates a URL request for the specified [url]. |
56 * | 90 * |
57 * By default this will do an HTTP GET request, this can be overridden with | 91 * By default this will do an HTTP GET request, this can be overridden with |
58 * [method]. | 92 * [method]. |
59 * | 93 * |
60 * The Future is completed when the response is available. | 94 * The Future is completed when the response is available. |
61 * | 95 * |
62 * The [withCredentials] parameter specified that credentials such as a cookie | 96 * The [withCredentials] parameter specified that credentials such as a cookie |
63 * (already) set in the header or | 97 * (already) set in the header or |
64 * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2) | 98 * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2) |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 $if DART2JS | 216 $if DART2JS |
183 var xhr = new HttpRequest(); | 217 var xhr = new HttpRequest(); |
184 return JS('bool', '("overrideMimeType" in #)', xhr); | 218 return JS('bool', '("overrideMimeType" in #)', xhr); |
185 $else | 219 $else |
186 return true; | 220 return true; |
187 $endif | 221 $endif |
188 } | 222 } |
189 | 223 |
190 $!MEMBERS | 224 $!MEMBERS |
191 } | 225 } |
OLD | NEW |