-
Notifications
You must be signed in to change notification settings - Fork 1
Sending Requests
Dickson Law edited this page Apr 9, 2023
·
2 revisions
-
xhr_request(verb, url, body, options): Send off a HTTP request, then return the asynchronous request ID.
These functions are shorthands for sending requests with specific verbs.
xhr_get(url, options)xhr_head(url, options)xhr_options(url, options)xhr_trace(url, options)
The body may be specified as an untyped struct or one of the request body class types.
xhr_delete(url, body, options)xhr_patch(url, body, options)xhr_post(url, body, options)xhr_put(url, body, options)
The options parameter is an untyped struct specifying additional properties for the request and what to do while handling the response. Available keys are:
-
decoder: Specify a method that accepts the response as a string and returns a decoded result. If specified asundefined, the response will be returned as-is. Leave unspecified to use the default set byREQM_DEFAULT_DECODER -
done: Specify a callback method that accepts aXhrResponsestruct and performs actions on it when successful. -
encoder: Specify a function that accepts a struct and returns a typed struct containingsetHeader(map),addKey(k, v),getBody()andcleanBody(body)methods. Only applicable when a plain struct is passed as the body. -
fail: Specify a callback method that accepts aXhrResponsestruct and performs actions on it when the request is unsuccessful and/or the decoder throws an exception. -
headers: Specify a map ID or array with pairwise header-value entries, and add those to the request's headers. For example, specifying[["Accept", "application/json"], ["Accept-Charset", "utf-8"]]here will ask the server to respond in UTF-8 JSON if able. -
params: If specified as an untyped struct, it will be URL-encoded and appended to the URL. For example, specifying{q: "Hello World"}here will append?q=Hello%20Worldto the URL. -
progress: Specify a callback method that accepts the current download progress and the total size of the download, and performs actions on it when the request is in progress. -
subject: Specify an instance ID to bind callback methods to. The daemon will also monitor its existence, and self-destruct if it no longer exists. If specified asnoone, the daemon will not monitor anything else. Leave unspecified to use the default set byREQM_DEFALT_SUBJECT.
The done and fail options take XhrResponse structs, which describe the outcome of the request.
-
data: The decoded result of the response. If decode was unsuccessful, this will containundefined. -
decodeOk: Whether the decode was successful. -
decodeException: (if decode failed) The exception thrown by the decoder. -
handle: The asynchronous request ID. -
headers: A map containing the headers of the response. -
httpStatus: The HTTP status code of the response (e.g. 200). -
url: The URL being requested.
-
isSuccessful(): Return whether the request is considered successful (decode is OK, HTTP status is 2xx)