HttpRequest class - dart:html library (original) (raw)

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

Note: You should avoid directly using HttpRequest to make HTTP requests. You can use HttpRequest indirectly through theBrowserClientadapter in package:http.

Using a higher-level library, likepackage:http, allows you to switch implementations with minimal changes to your code. For example,package:http Clienthas implementations for the browser and implementations that use platform native HTTP clients on Android and iOS.

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

Inheritance

Annotations

Constructors

HttpRequest()

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

factory

Properties

hashCodeint

The hash code for this object.

no setterinherited

onEvents

This is an ease-of-use accessor for event streams which should only be used when an explicit accessor is not available.

no setterinherited

onAbortStream<ProgressEvent>

Stream of abort events handled by this HttpRequestEventTarget.

no setterinherited

onErrorStream<ProgressEvent>

Stream of error events handled by this HttpRequestEventTarget.

no setterinherited

onLoadStream<ProgressEvent>

Stream of load events handled by this HttpRequestEventTarget.

no setterinherited

onLoadEndStream<ProgressEvent>

Stream of loadend events handled by this HttpRequestEventTarget.

no setterinherited

onLoadStartStream<ProgressEvent>

Stream of loadstart events handled by this HttpRequestEventTarget.

no setterinherited

onProgressStream<ProgressEvent>

Stream of progress events handled by this HttpRequestEventTarget.

no setterinherited

onReadyStateChangeStream<Event>

Event listeners to be notified every time the HttpRequestobject's readyState changes values.

no setter

onTimeoutStream<ProgressEvent>

Stream of timeout events handled by this HttpRequestEventTarget.

no setterinherited

readyStateint

Indicator of the current state of the request:

no setter

response → dynamic

The data received as a reponse from the request.

no setter

Returns all response headers as a key-value map.

no setter

responseTextString?

The response in String form or empty String on failure.

no setter

responseTypeString

String telling the server the desired response format.

getter/setter pair

responseUrlString?

no setter

responseXmlDocument?

The request response, or null on failure.

no setter

runtimeTypeType

A representation of the runtime type of the object.

no setterinherited

statusint?

The HTTP result code from the request (200, 404, etc). See also: HTTP Status Codes

no setter

statusTextString?

The request response string (such as "OK"). See also: HTTP Status Codes

no setter

timeoutint?

Length of time in milliseconds before a request is automatically terminated.

getter/setter pair

uploadHttpRequestUpload

EventTarget that can hold listeners to track the progress of the request.

no setter

withCredentialsbool?

True if cross-site requests should use credentials such as cookies or authorization headers; false otherwise.

getter/setter pair

Methods

abort()→ void

Stop the current request.

addEventListener(String type, EventListener? listener, [bool? useCapture])→ void

inherited

dispatchEvent(Event event)→ bool

inherited

Retrieve all the response headers from a request.

Return the response header named header, or null if not found.

noSuchMethod(Invocation invocation)→ dynamic

Invoked when a nonexistent method or property is accessed.

inherited

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.

removeEventListener(String type, EventListener? listener, [bool? useCapture])→ void

inherited

send([dynamic body_OR_data])→ void

Send the request with any given data.

Sets the value of an HTTP request header.

toString()→ String

A string representation of this object.

inherited

Operators

operator ==(Object other)→ bool

The equality operator.

inherited

Static Properties

supportsCrossOriginbool

Checks to see if the current platform supports making cross origin requests.

no setter

supportsLoadEndEventbool

Checks to see if the LoadEnd event is supported on the current platform.

no setter

supportsOverrideMimeTypebool

Checks to see if the overrideMimeType method is supported on the current platform.

no setter

supportsProgressEventbool

Checks to see if the Progress event is supported on the current platform.

no setter

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, 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, 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

LOADING → const int

OPENED → const int

readyStateChangeEvent → const EventStreamProvider<Event>

Static factory designed to expose readystatechange events to event handlers that are not necessarily instances of HttpRequest.

UNSENT → const int