# HttpRequest class

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

- Fetch Data Dynamically, a tutorial from A Game of Darts, shows two different ways to use HttpRequest to get a JSON file.
- Get Input from a Form, another tutorial from A Game of Darts, shows using HttpRequest with a custom server.
- Dart article on using HttpRequests
- JS XMLHttpRequest
- Using XMLHttpRequest

Inheritance
  - Object
  - EventTarget
  - HttpRequestEventTarget
  - HttpRequest
Annotations
  - @Native("XMLHttpRequest")

## Constructors

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

## Properties

onReadyStateChange → Stream<Event>
  Event listeners to be notified every time the HttpRequest object's `readyState` changes values.
  read-only
readyState → int
  Indicator of the current state of the request: [...]
  final
response → dynamic
  The data received as a reponse from the request. [...]
  @SupportedBrowser(SupportedBrowser.CHROME), @SupportedBrowser(SupportedBrowser.FIREFOX), @SupportedBrowser(SupportedBrowser.IE, '10'), @SupportedBrowser(SupportedBrowser.SAFARI), read-only
responseHeaders → Map<String, String>
  Returns all response headers as a key-value map. [...]
  read-only
responseText → String
  The response in String form or empty String on failure.
  final
responseType ↔ String
  String telling the server the desired response format. [...]
  read / write
responseUrl → String
  @JSName('responseURL'), final
responseXml → Document
  The request response, or null on failure. [...]
  @JSName('responseXML'), final
status → int
  The HTTP result code from the request (200, 404, etc). See also: HTTP Status Codes
  final
statusText → String
  The request response string (such as "200 OK"). See also: HTTP Status Codes
  final
timeout ↔ int
  Length of time in milliseconds before a request is automatically terminated. [...]
  read / write
upload → HttpRequestUpload
  EventTarget that can hold listeners to track the progress of the request. The events fired will be members of `HttpRequestUploadEvents`.
  @Unstable(), final
withCredentials ↔ bool
  True if cross-site requests should use credentials such as cookies or authorization headers; false otherwise. [...]
  read / write
hashCode → int
  The hash code for this object. [...]
  read-only, inherited
on → Events
  This is an ease-of-use accessor for event streams which should only be used when an explicit accessor is not available.
  read-only, inherited
onAbort → Stream<ProgressEvent>
  Stream of `abort` events handled by this HttpRequestEventTarget.
  read-only, inherited
onError → Stream<ProgressEvent>
  Stream of `error` events handled by this HttpRequestEventTarget.
  read-only, inherited
onLoad → Stream<ProgressEvent>
  Stream of `load` events handled by this HttpRequestEventTarget.
  read-only, inherited
onLoadEnd → Stream<ProgressEvent>
  Stream of `loadend` events handled by this HttpRequestEventTarget.
  @SupportedBrowser(SupportedBrowser.CHROME), @SupportedBrowser(SupportedBrowser.FIREFOX), @SupportedBrowser(SupportedBrowser.IE, '10'), @SupportedBrowser(SupportedBrowser.SAFARI), read-only, inherited
onLoadStart → Stream<ProgressEvent>
  Stream of `loadstart` events handled by this HttpRequestEventTarget.
  read-only, inherited
onProgress → Stream<ProgressEvent>
  Stream of `progress` events handled by this HttpRequestEventTarget.
  @SupportedBrowser(SupportedBrowser.CHROME), @SupportedBrowser(SupportedBrowser.FIREFOX), @SupportedBrowser(SupportedBrowser.IE, '10'), @SupportedBrowser(SupportedBrowser.SAFARI), read-only, inherited
onTimeout → Stream<ProgressEvent>
  Stream of `timeout` events handled by this HttpRequestEventTarget.
  read-only, inherited
runtimeType → Type
  A representation of the runtime type of the object.
  read-only, inherited

## Methods

abort() → void
  Stop the current request. [...]
getAllResponseHeaders() → String
  Retrieve all the response headers from a request. [...]
  @Unstable()
getResponseHeader(String name) → String
  Return the response header named `header`, or null if not found. [...]
  @Unstable()
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 mime) → void
  Specify a particular MIME type (such as `text/xml`) desired for the response. [...]
  @SupportedBrowser(SupportedBrowser.CHROME), @SupportedBrowser(SupportedBrowser.FIREFOX), @SupportedBrowser(SupportedBrowser.SAFARI)
send([dynamic body_OR_data ]) → void
  Send the request with any given `data`. [...]
setRequestHeader(String name, String value) → void
  Sets the value of an HTTP request header. [...]
addEventListener(String type, EventListener listener, [ bool useCapture ]) → void
  inherited
dispatchEvent(Event event) → bool
  inherited
noSuchMethod(Invocation invocation) → dynamic
  Invoked when a non-existent method or property is accessed. [...]
  inherited
removeEventListener(String type, EventListener listener, [ bool useCapture ]) → void
  inherited
toString() → String
  Returns a string representation of this object.
  inherited

## Operators

operator ==(dynamic other) → bool
  The equality operator. [...]
  inherited

## Static Properties

supportsCrossOrigin → bool
  Checks to see if the current platform supports making cross origin requests. [...]
  read-only
supportsLoadEndEvent → bool
  Checks to see if the LoadEnd event is supported on the current platform.
  read-only
supportsOverrideMimeType → bool
  Checks to see if the overrideMimeType method is supported on the current platform.
  read-only
supportsProgressEvent → bool
  Checks to see if the Progress event is supported on the current platform.
  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, dynamic 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. [...]

## Constants

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