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
- Annotations
- DomName('XMLHttpRequest')
- Extends
- Object
- EventTarget
- HttpRequestEventTarget
- HttpRequest
Constants
- int DONE = 4
-
const
- int HEADERS_RECEIVED = 2
-
const
- int LOADING = 3
-
const
- int OPENED = 1
-
const
-
EventStreamProvider<ProgressEvent>
readyStateChangeEvent
=
const EventStreamProvider<ProgressEvent>
('readystatechange') -
constStatic factory designed to expose
readystatechange
events to event handlers that are not necessarily instances of HttpRequest. - int UNSENT = 0
-
const
Static Properties
- bool supportsCrossOrigin
-
read-onlyChecks to see if the current platform supports making cross origin requests.
- bool supportsLoadEndEvent
-
read-onlyChecks to see if the LoadEnd event is supported on the current platform.
- bool supportsOverrideMimeType
-
read-onlyChecks to see if the overrideMimeType method is supported on the current platform.
- bool supportsProgressEvent
-
read-onlyChecks to see if the Progress event is supported on the current platform.
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.
Properties
- Events on
-
read-only, inheritedThis is an ease-of-use accessor for event streams which should only be used when an explicit accessor is not available.
- Stream<ProgressEvent> onAbort
-
read-only, inheritedStream of
abort
events handled by thisHttpRequestEventTarget
. - Stream<ProgressEvent> onError
-
read-only, inheritedStream of
error
events handled by thisHttpRequestEventTarget
. - Stream<ProgressEvent> onLoad
-
read-only, inheritedStream of
load
events handled by thisHttpRequestEventTarget
. - Stream<ProgressEvent> onLoadEnd
-
read-only, inheritedStream of
loadend
events handled by thisHttpRequestEventTarget
. - Stream<ProgressEvent> onLoadStart
-
read-only, inheritedStream of
loadstart
events handled by thisHttpRequestEventTarget
. - Stream<ProgressEvent> onProgress
-
read-only, inheritedStream of
progress
events handled by thisHttpRequestEventTarget
. - Stream<ProgressEvent> onReadyStateChange
-
read-onlyEvent listeners to be notified every time the
HttpRequest
object'sreadyState
changes values. - Stream<ProgressEvent> onTimeout
-
read-only, inheritedStream of
timeout
events handled by thisHttpRequestEventTarget
. - int readyState
-
read-onlyIndicator of the current state of the request:
- Object response
-
read-onlyThe data received as a reponse from the request.
- Map<String,String> responseHeaders
-
read-onlyReturns all response headers as a key-value map.
- String responseText
-
read-onlyThe response in String form or empty String on failure.
- String responseType
-
read / write
String
telling the server the desired response format. - String responseUrl
-
read-only
- Document responseXml
-
read-onlyThe request response, or null on failure.
- int status
-
read-onlyThe http result code from the request (200, 404, etc). See also: Http Status Codes
- String statusText
-
read-onlyThe request response string (such as \"200 OK\"). See also: Http Status Codes
- int timeout
-
read / writeLength of time before a request is automatically terminated.
- HttpRequestUpload upload
-
read-only
EventTarget
that can hold listeners to track the progress of the request. The events fired will be members ofHttpRequestUploadEvents
. - bool withCredentials
-
read / writeTrue if cross-site requests should use credentials such as cookies or authorization headers; false otherwise.
Constructors
- HttpRequest()
- General constructor for any type of request (GET, POST, etc).
Methods
-
abort(
) → void - Stop the current request.
-
addEventListener(
String type, dynamic listener(Event event), [bool useCapture]) → void -
inherited
-
dispatchEvent(
Event event) → bool -
inherited
-
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
, andmethod
to use in making the request. -
overrideMimeType(
String override) → void -
Specify a particular MIME type (such as
text/xml
) desired for the response. -
removeEventListener(
String type, dynamic listener(Event event), [bool useCapture]) → void -
inherited
-
send(
[data]) → void -
Send the request with any given
data
. -
setRequestHeader(
String header, String value) → void - Sets the value of an HTTP requst header.