Class QfXmlHttpRequest
#include <qfxmlhttprequest.h>
Inherits the following classes: QObject
Public Types
| Type | Name |
|---|---|
| enum uchar | ReadyState |
Public Properties
| Type | Name |
|---|---|
| property QJSValue | onaborted |
| property QJSValue | ondownloadprogress |
| property QJSValue | onerror |
| property QJSValue | onload |
| property QJSValue | onloadend |
| property QJSValue | onreadystatechange |
| property QJSValue | onredirected |
| property QJSValue | ontimeout |
| property QJSValue | onuploadprogress |
| property ReadyState | readyState |
| property QVariant | response |
| property QString | responseText |
| property QString | responseType |
| property QUrl | responseUrl |
| property int | status |
| property QString | statusText |
| property int | timeout |
Public Signals
| Type | Name |
|---|---|
| signal void | readyStateChanged |
| signal void | responseChanged Emitted whenever response-related properties change. |
| signal void | timeoutChanged |
Public Functions
| Type | Name |
|---|---|
| QfXmlHttpRequest (QObject * parent=nullptr) |
|
| Q_INVOKABLE void | abort () |
| Q_INVOKABLE QString | getAllResponseHeaders () const |
| Q_INVOKABLE QString | getResponseHeader (const QString & name) const |
| QJSValue | onaborted () const |
| QJSValue | ondownloadprogress () const |
| QJSValue | onerror () const |
| QJSValue | onload () const |
| QJSValue | onloadend () const |
| QJSValue | onreadystatechange () const |
| QJSValue | onredirected () const |
| QJSValue | ontimeout () const |
| QJSValue | onuploadprogress () const |
| Q_INVOKABLE void | open (const QString & method, const QUrl & url) |
| ReadyState | readyState () const |
| QVariant | response () const |
| QString | responseText () const |
| QString | responseType () const |
| QUrl | responseUrl () const |
| Q_INVOKABLE void | send (const QVariant & body=QVariant()) |
| void | setOnaborted (const QJSValue & callback) |
| void | setOndownloadprogress (const QJSValue & callback) |
| void | setOnerror (const QJSValue & callback) |
| void | setOnload (const QJSValue & callback) |
| void | setOnloadend (const QJSValue & callback) |
| void | setOnreadystatechange (const QJSValue & callback) |
| void | setOnredirected (const QJSValue & callback) |
| void | setOntimeout (const QJSValue & callback) |
| void | setOnuploadprogress (const QJSValue & callback) |
| Q_INVOKABLE void | setRequestHeader (const QString & headerName, const QString & headerValue) |
| void | setTimeout (int milliseconds) |
| int | status () const |
| QString | statusText () const |
| int | timeout () const |
| ~QfXmlHttpRequest () override |
Public Static Functions
| Type | Name |
|---|---|
| Q_INVOKABLE QfXmlHttpRequest * | newRequest (QObject * parent=nullptr) |
Detailed Description
This class exposes a lightweight, XMLHttpRequest-like API to QML for making HTTP requests in a predictable way. It follows the familiar open(), setRequestHeader(), send(), and abort() workflow while exposing request state, response data, and lifecycle callbacks as QML properties.
Payload encoding is deterministic. QByteArray payloads are transmitted as-is. QString payloads are sent as UTF-8 text. QVariantMap and QVariantList payloads are encoded as JSON. When the caller does not explicitly set a Content-Type header, an appropriate Content-Type is inferred from the payload type.
Multipart uploads are supported when the body contains "file://..." URL strings, or when the caller sets a Content-Type header starting with "multipart/form-data". Local file uploads are restricted to paths inside the current project directory or inside the QFieldCloud local directory.
Requests created for QML usage are expected to be short-lived. The object schedules deleteLater() automatically once the request lifecycle ends (success, error, abort, or timeout).
Note:
This is not a full browser XMLHttpRequest implementation. It focuses on a clean QML API and predictable behavior within QField.
Public Types Documentation
enum ReadyState
enum QfXmlHttpRequest::ReadyState {
Unsent = 0,
Opened,
HeadersReceived,
Loading,
Done
};
ReadyState values aligned with browser XMLHttpRequest.
Public Properties Documentation
property onaborted [1/2]
QJSValue QfXmlHttpRequest::onaborted;
Called when the request is aborted.
property ondownloadprogress [1/2]
QJSValue QfXmlHttpRequest::ondownloadprogress;
Called during download progress as (receivedBytes, totalBytes).
property onerror [1/2]
QJSValue QfXmlHttpRequest::onerror;
Called for network-level failures as (errorCode, errorMessage).
property onload [1/2]
QJSValue QfXmlHttpRequest::onload;
Called when the request completes and a status code is available.
Note:
HTTP errors such as 404 or 500 still trigger onload. Callers should inspect status.
property onloadend [1/2]
QJSValue QfXmlHttpRequest::onloadend;
Called at the end of the request lifecycle for success, error, abort, or timeout.
property onreadystatechange [1/2]
QJSValue QfXmlHttpRequest::onreadystatechange;
Called whenever readyState changes.
property onredirected [1/2]
QJSValue QfXmlHttpRequest::onredirected;
Called when a redirect is reported by Qt as (urlString).
property ontimeout [1/2]
QJSValue QfXmlHttpRequest::ontimeout;
Called when the timeout triggers.
property onuploadprogress [1/2]
QJSValue QfXmlHttpRequest::onuploadprogress;
Called during upload progress as (sentBytes, totalBytes).
property readyState [1/2]
ReadyState QfXmlHttpRequest::readyState;
The current request state aligned with browser XMLHttpRequest readyState values.
property response [1/2]
QVariant QfXmlHttpRequest::response;
The parsed response value when the response is JSON, otherwise this equals responseText.
property responseText [1/2]
QString QfXmlHttpRequest::responseText;
The response body decoded as UTF-8 text.
property responseType [1/2]
QString QfXmlHttpRequest::responseType;
The response Content-Type value as provided by the server.
property responseUrl [1/2]
QUrl QfXmlHttpRequest::responseUrl;
The final response URL. This may differ from the requested URL when redirects were followed.
property status [1/2]
int QfXmlHttpRequest::status;
The HTTP status code of the final response. For network-level failures, this is 0.
property statusText [1/2]
QString QfXmlHttpRequest::statusText;
The HTTP reason phrase of the final response when available.
property timeout [1/2]
int QfXmlHttpRequest::timeout;
The request timeout in milliseconds. A value of 0 disables the timeout.
When the timeout triggers, the request is aborted and ontimeout and onloadend are called.
Public Signals Documentation
signal readyStateChanged
void QfXmlHttpRequest::readyStateChanged;
The current request state aligned with browser XMLHttpRequest readyState values.
signal responseChanged
Emitted whenever response-related properties change.
void QfXmlHttpRequest::responseChanged;
signal timeoutChanged
void QfXmlHttpRequest::timeoutChanged;
The request timeout in milliseconds. A value of 0 disables the timeout.
When the timeout triggers, the request is aborted and ontimeout and onloadend are called.
Public Functions Documentation
function QfXmlHttpRequest
explicit QfXmlHttpRequest::QfXmlHttpRequest (
QObject * parent=nullptr
)
function abort
Q_INVOKABLE void QfXmlHttpRequest::abort ()
Aborts the current request if one is active.
Note:
This triggers onaborted and onloadend if set and transitions readyState to Done.
function getAllResponseHeaders
Q_INVOKABLE QString QfXmlHttpRequest::getAllResponseHeaders () const
Returns all response headers in HTTP-style format. This is available only after the request finishes.
function getResponseHeader
Q_INVOKABLE QString QfXmlHttpRequest::getResponseHeader (
const QString & name
) const
Returns a response header value by name. This is available only after the request finishes.
function onaborted [2/2]
inline QJSValue QfXmlHttpRequest::onaborted () const
Called when the request is aborted.
function ondownloadprogress [2/2]
inline QJSValue QfXmlHttpRequest::ondownloadprogress () const
Called during download progress as (receivedBytes, totalBytes).
function onerror [2/2]
inline QJSValue QfXmlHttpRequest::onerror () const
Called for network-level failures as (errorCode, errorMessage).
function onload [2/2]
inline QJSValue QfXmlHttpRequest::onload () const
Called when the request completes and a status code is available.
Note:
HTTP errors such as 404 or 500 still trigger onload. Callers should inspect status.
function onloadend [2/2]
inline QJSValue QfXmlHttpRequest::onloadend () const
Called at the end of the request lifecycle for success, error, abort, or timeout.
function onreadystatechange [2/2]
inline QJSValue QfXmlHttpRequest::onreadystatechange () const
Called whenever readyState changes.
function onredirected [2/2]
inline QJSValue QfXmlHttpRequest::onredirected () const
Called when a redirect is reported by Qt as (urlString).
function ontimeout [2/2]
inline QJSValue QfXmlHttpRequest::ontimeout () const
Called when the timeout triggers.
function onuploadprogress [2/2]
inline QJSValue QfXmlHttpRequest::onuploadprogress () const
Called during upload progress as (sentBytes, totalBytes).
function open
Q_INVOKABLE void QfXmlHttpRequest::open (
const QString & method,
const QUrl & url
)
Prepares the request. This must be called before send().
Calling open() while a request is active aborts the active request. Supported methods are GET, POST, PUT, and DELETE.
function readyState [2/2]
inline ReadyState QfXmlHttpRequest::readyState () const
The current request state aligned with browser XMLHttpRequest readyState values.
function response [2/2]
inline QVariant QfXmlHttpRequest::response () const
The parsed response value when the response is JSON, otherwise this equals responseText.
function responseText [2/2]
inline QString QfXmlHttpRequest::responseText () const
The response body decoded as UTF-8 text.
function responseType [2/2]
inline QString QfXmlHttpRequest::responseType () const
The response Content-Type value as provided by the server.
function responseUrl [2/2]
inline QUrl QfXmlHttpRequest::responseUrl () const
The final response URL. This may differ from the requested URL when redirects were followed.
function send
Q_INVOKABLE void QfXmlHttpRequest::send (
const QVariant & body=QVariant()
)
Starts the request.
The body can be provided as QByteArray, QString, QVariantMap, or QVariantList. QByteArray is sent as-is, QString is encoded as UTF-8 text, and QVariantMap/QVariantList are JSON-encoded. If multipart upload is desired, the caller can pass a QVariantMap containing "file://..." string values, or can set a Content-Type header starting with "multipart/form-data". Local file URLs are accepted only when they resolve inside the current project directory or the QFieldCloud local directory.
function setOnaborted
inline void QfXmlHttpRequest::setOnaborted (
const QJSValue & callback
)
Called when the request is aborted.
function setOndownloadprogress
inline void QfXmlHttpRequest::setOndownloadprogress (
const QJSValue & callback
)
Called during download progress as (receivedBytes, totalBytes).
function setOnerror
inline void QfXmlHttpRequest::setOnerror (
const QJSValue & callback
)
Called for network-level failures as (errorCode, errorMessage).
function setOnload
inline void QfXmlHttpRequest::setOnload (
const QJSValue & callback
)
Called when the request completes and a status code is available.
Note:
HTTP errors such as 404 or 500 still trigger onload. Callers should inspect status.
function setOnloadend
inline void QfXmlHttpRequest::setOnloadend (
const QJSValue & callback
)
Called at the end of the request lifecycle for success, error, abort, or timeout.
function setOnreadystatechange
inline void QfXmlHttpRequest::setOnreadystatechange (
const QJSValue & callback
)
Called whenever readyState changes.
function setOnredirected
inline void QfXmlHttpRequest::setOnredirected (
const QJSValue & callback
)
Called when a redirect is reported by Qt as (urlString).
function setOntimeout
inline void QfXmlHttpRequest::setOntimeout (
const QJSValue & callback
)
Called when the timeout triggers.
function setOnuploadprogress
inline void QfXmlHttpRequest::setOnuploadprogress (
const QJSValue & callback
)
Called during upload progress as (sentBytes, totalBytes).
function setRequestHeader
Q_INVOKABLE void QfXmlHttpRequest::setRequestHeader (
const QString & headerName,
const QString & headerValue
)
Sets a raw HTTP request header.
Headers are only applied after open() and before send(). Newline protection is applied to prevent CR/LF header injection.
function setTimeout
void QfXmlHttpRequest::setTimeout (
int milliseconds
)
The request timeout in milliseconds. A value of 0 disables the timeout.
When the timeout triggers, the request is aborted and ontimeout and onloadend are called.
function status [2/2]
inline int QfXmlHttpRequest::status () const
The HTTP status code of the final response. For network-level failures, this is 0.
function statusText [2/2]
inline QString QfXmlHttpRequest::statusText () const
The HTTP reason phrase of the final response when available.
function timeout [2/2]
inline int QfXmlHttpRequest::timeout () const
The request timeout in milliseconds. A value of 0 disables the timeout.
When the timeout triggers, the request is aborted and ontimeout and onloadend are called.
function ~QfXmlHttpRequest
QfXmlHttpRequest::~QfXmlHttpRequest () override
Public Static Functions Documentation
function newRequest
static Q_INVOKABLE QfXmlHttpRequest * QfXmlHttpRequest::newRequest (
QObject * parent=nullptr
)
Creates a new request instance.
The documentation for this class was generated from the following file src/core/qfxmlhttprequest.h