HttpRequest class

Warning

This class is deprecated and will be removed in a future release of package:web.

You should instead use the cross-platform package:http and its BrowserClient adapter on top of XMLHttpRequest.

A helper used to make it easier to operate over XMLHttpRequests.

The logic here was copied from dart:html to help bridge a functionality gap missing in package:web.

HttpRequest can be used to obtain data from HTTP and FTP protocols, and is useful for AJAX-style page updates.

The simplest way to get the contents of a text file, such as a JSON-formatted file, is with getString. For example, the following code gets the contents of a JSON file and prints its length:

var path = 'myData.json';
HttpRequest.getString(path).then((String fileContents) {
  print(fileContents.length);
}).catchError((error) {
  print(error.toString());
});

Fetching data from other servers

For security reasons, browsers impose restrictions on requests made by embedded apps. With the default behavior of this class, the code making the request must be served from the same origin (domain name, port, and application layer protocol) as the requested resource. In the example above, the myData.json file must be co-located with the app that uses it.

Other resources

Annotations

Constructors

HttpRequest()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

getString(String url, {bool? withCredentials, void onProgress(ProgressEvent)?}) Future<String>
Creates a GET request for the specified url.
postFormData(String url, Map<String, String> data, {bool? withCredentials, String? responseType, Map<String, String>? requestHeaders, void onProgress(ProgressEvent)?}) Future<XMLHttpRequest>
Makes a server POST request with the specified data encoded as form data.
request(String url, {String? method, bool? withCredentials, String? responseType, String? mimeType, Map<String, String>? requestHeaders, Object? sendData, void onProgress(ProgressEvent)?}) Future<XMLHttpRequest>
Creates and sends a URL request for the specified url.

Constants

DONE → const int
HEADERS_RECEIVED → const int
LOADING → const int
OPENED → const int
UNSENT → const int