HttpRequest class

A client-side XHR request for getting data from a URL, formally known as XMLHttpRequest.

A client-side XHR request for getting data from a URL, formally known as XMLHttpRequest.

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 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. You might be able to get around this restriction by using CORS headers or JSONP.

Other resources

Annotated by:
  • DomName('XMLHttpRequest')
Extends:

Constructors

HttpRequest ( )
General constructor for any type of request (GET, POST, etc).

Constants

readyStateChangeEvent = const EventStreamProvider<ProgressEvent>('readystatechange')
Static factory designed to expose readystatechange events to event handlers that are not necessarily instances of HttpRequest.
DONE = 4
HEADERS_RECEIVED = 2
LOADING = 3
OPENED = 1
UNSENT = 0

Static Properties

supportsProgressEvent bool
read-only
supportsCrossOrigin bool
read-only
supportsLoadEndEvent bool
read-only
supportsOverrideMimeType bool
read-only

Static Methods

getString ( String url, {bool withCredentials, void onProgress(ProgressEvent e)} ) → 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 e)} ) → Future<HttpRequest>
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, sendData, void onProgress(ProgressEvent e)} ) → Future<HttpRequest>
Creates and sends a URL request for the specified url.
requestCrossOrigin ( String url, {String method, String sendData} ) → Future<String>
Makes a cross-origin request to the specified URL.

Instance Properties

responseHeaders Map<String,String>
read-only
readyState int
read-only
response Object
read-only
responseText String
read-only
responseType String
read/write
responseUrl String
read-only
responseXml Document
read-only
status int
read-only
statusText String
read-only
timeout int
read/write
upload HttpRequestUpload
read-only
withCredentials bool
read/write
onReadyStateChange Stream<ProgressEvent>
read-only
on Events Inherited
read-only
onAbort Stream<ProgressEvent> Inherited
read-only
onError Stream<ProgressEvent> Inherited
read-only
onLoad Stream<ProgressEvent> Inherited
read-only
onLoadEnd Stream<ProgressEvent> Inherited
read-only
onLoadStart Stream<ProgressEvent> Inherited
read-only
onProgress Stream<ProgressEvent> Inherited
read-only
onTimeout Stream<ProgressEvent> Inherited
read-only

Instance Methods

abort ( ) → void
Stop the current request.
getAllResponseHeaders ( ) → String
Retrieve all the response headers from a request.
getResponseHeader ( String header ) → String
Return the response header named header, or null if not found.
open ( String method, String url, {bool async, String user, String password} ) → void
Specify the desired url, and method to use in making the request.
overrideMimeType ( String override ) → void
Specify a particular MIME type (such as text/xml) desired for the response.
send ( [data] ) → void
Send the request with any given data.
setRequestHeader ( String header, String value ) → void
Sets the value of an HTTP requst header.
addEventListener Inherited ( String type, dynamic listener(Event event), [bool useCapture] ) → void
removeEventListener Inherited ( String type, dynamic listener(Event event), [bool useCapture] ) → void
dispatchEvent Inherited ( Event event ) → bool