v1.2.0.1-beta1

Description

The Request interface of the Fetch API represents a resource request.

Request objects can be created directly, or implicitly by passing an initializer object to Net.fetch() or Net.request() as the options argument.

Additionally, request() will return a Request object, which has not been sent yet. The request can then be sent via one of the methods get(), head(), post(), or put().

Note
Request has a static member object named Request.GlobalDefaults which can be used to set default values on any created Request (either implicitly created in a call to fetch() or explicitly with new Request()). Simply set this variable somewhere in your script with whichever default options you would like to always be set (or set them individually with Request.GlobalDefaults.property = value). Any/all of the properties described below for the Request object can be set this way.

Properties

any body = null
 
string credentials = "include"
 
Headers headers = {}
 
string method = "GET"
 
bool noThrow = false
 
function onprogress = null
 
string redirect = "no-less-safe"
 
bool rejectOnError = false
 
string responseType = ""
 
AbortSignal signal = null
 
int timeout = 30 * 1000
 
string url = ""
 
XMLHttpRequest xhr = null
 

Public Member Functions

< Promise|Responseget ()
 
< Promise|Responsehead ()
 
< Promise|Responsepost (data)
 
< Promise|Responseput (data)
 

Public Attributes

Object GlobalDefaults = {}
 

Property Documentation

◆ body

any body = null
read

Data for POST or PUT request.

◆ credentials

string credentials = "include"
readwrite

Credentials type to use for the request; Only omit and include are supported.

The default is include which is equivalent to setting XMLHttpRequest.withCredentials = true.

◆ headers

Headers headers = {}
readwrite

The Headers object associated with the response.

Headers can be specified a number of ways (this is the same as the standard ways of passing or creating Header objects):

  • By passing a populated Headers object (created with Headers(init)).
  • By passing a plain object of header name: value pairs, for example
    { 'Content-Type': 'application/json', 'Accept': 'text/json, application/json' }
  • With a 2-dimensional array of [name, value] pairs, for example:
    [
    [ 'Content-Type', 'application/json' ],
    [ 'Accept', 'text/json, application/json' ]
    ]
    See also
    https://developer.mozilla.org/en-US/docs/Web/API/Headers

◆ method

string method = "GET"
readwrite

HTTP method to use for the request.

One of:

  • GET
  • PUT
  • HEAD
  • POST
  • DELETE
  • OPTIONS
  • PROPFIND
  • PATCH

◆ noThrow

bool noThrow = false
readwrite

This options only applies to Net.request().

If set to true then Net.request() will not throw any exceptions in case of request failure, and instead just return whatever Result is available regardless of actual status. This is a non-standard option.

◆ onprogress

function onprogress = null
readwrite

Callback function for downnload/upload progress events.

The callback gets passed an object which contains the following properties:

  • lengthComputable - true or false indicating if the total size of the requested data is available.
  • loaded - bytes transferred so far.
  • total - total bytes to transfer, if known.

For more details see the XMLHttpRequest: progress event

◆ redirect

string redirect = "no-less-safe"
readwrite

Specifies redirect handling options.

The first 3 options are standard, the last 2 are implementation-specific:

  • follow: Automatically follow redirects. Unless otherwise stated the redirect mode is set to follow.
  • error: Abort with an error if a redirect occurs.
  • manual: Caller intends to process the response in another context.
  • no-less-safe: (Default) Only allow http -> http, http -> https, and https -> https redirects (do not follow https -> http redirect);
  • same-origin: Only follow redirect if the original scheme, host, and port match.

◆ rejectOnError

bool rejectOnError = false
readwrite

If set to true, Net.fetch() will reject the promise if the returned HTTP status code is not in the 200-299 range, the same way as if a network error occurred.

See Net.fetch() for details on the various response handling options. This is a non-standard option.

◆ responseType

string responseType = ""
readwrite

responseType property is an enumerated string value specifying the type of data contained in the body property (actually in XMLHttpRequest.response).

It also lets the author change the response type. If an empty string is set as the value of responseType, the default value of text is used. The following values are supported:

  • text - The default type, interprets the response as plain text.
  • arraybuffer - The body will be returned as an ArrayBuffer object of raw bytes (suitable for binary formats such as images or executables).
  • document - The response is an HTML or XML Document type. See XMLHttpRequest for details about the available Document properties.
  • json - The response body will be parsed as JSON data into a JavaScript Object or Array.

Note that the blob response type is not available due to lack of Blob support in the environment. Use "arraybuffer" instead.

◆ signal

AbortSignal signal = null
readwrite

Sets an AbortSignal which was obtained from an AbortController to use for aborting the network request.

This is analogous to the standard usage of this property. See AbortController @ MDN for general information and the local documentation for some implementation details.

◆ timeout

int timeout = 30 * 1000
readwrite

The timeout for the network request, in milliseconds.

In case of timeout, a TimeoutError is delivered first, followed by the generic NetworkError type.

◆ url

string url = ""
readwrite

The URL of the request.

This is usually set in the Net.fetch(url) or Net.request(url) 'url' parameter.

◆ xhr

XMLHttpRequest xhr = null
readwrite

This is the underlying XMLHttpRequest object which will actually handle the request.

This is usually set up automatically by Net.fetch() or Net.request(), but an existing XMLHttpRequest could be provided here instead. The XMLHttpRequest must be in UNSENT or OPENED readyState.

Member Function Documentation

◆ get()

< Promise|Response > get ( )

Performs a "GET" request on the current url.

Returns
  • If async property is true then a Promise object, same as Net.fetch() would return.
  • If async property is false then a Response object, same as Net.request() would return.

◆ head()

< Promise|Response > head ( )

Performs a "HEAD" request on the current url.

Returns
  • If async property is true then a Promise object, same as Net.fetch() would return.
  • If async property is false then a Response object, same as Net.request() would return.

◆ post()

< Promise|Response > post ( data  )

Performs a "POST" request on the current url.

Parameters
dataThe data to send in the post request body, if any.
Returns
  • If async property is true then a Promise object, same as Net.fetch() would return.
  • If async property is false then a Response object, same as Net.request() would return.

◆ put()

< Promise|Response > put ( data  )

Performs a "PUT" request on the current url.

Parameters
dataThe data to send in the put request body, if any.
Returns
  • If async property is true then a Promise object, same as Net.fetch() would return.
  • If async property is false then a Response object, same as Net.request() would return.

Member Data Documentation

◆ GlobalDefaults

Object GlobalDefaults = {}

This global static object can be used to set default values on any created Request (either implicitly created in a call to fetch() or explicitly with new Request()).

Simply set this variable somewhere in your script with whichever default options you would like to always be set (or set them individually with Request.GlobalDefaults.property = value). Any/all of the properties described below for the Request object can be set this way.

Since
v1.2