Skip to content

Class QFieldXmlHttpRequest

ClassList > QFieldXmlHttpRequest

More...

  • #include <qfieldxmlhttprequest.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
QFieldXmlHttpRequest (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
~QFieldXmlHttpRequest () override

Public Static Functions

Type Name
Q_INVOKABLE QFieldXmlHttpRequest * 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 QFieldXmlHttpRequest::ReadyState {
    Unsent = 0,
    Opened,
    HeadersReceived,
    Loading,
    Done
};

ReadyState values aligned with browser XMLHttpRequest.


Public Properties Documentation

property onaborted [1/2]

QJSValue QFieldXmlHttpRequest::onaborted;

Called when the request is aborted.


property ondownloadprogress [1/2]

QJSValue QFieldXmlHttpRequest::ondownloadprogress;

Called during download progress as (receivedBytes, totalBytes).


property onerror [1/2]

QJSValue QFieldXmlHttpRequest::onerror;

Called for network-level failures as (errorCode, errorMessage).


property onload [1/2]

QJSValue QFieldXmlHttpRequest::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 QFieldXmlHttpRequest::onloadend;

Called at the end of the request lifecycle for success, error, abort, or timeout.


property onreadystatechange [1/2]

QJSValue QFieldXmlHttpRequest::onreadystatechange;

Called whenever readyState changes.


property onredirected [1/2]

QJSValue QFieldXmlHttpRequest::onredirected;

Called when a redirect is reported by Qt as (urlString).


property ontimeout [1/2]

QJSValue QFieldXmlHttpRequest::ontimeout;

Called when the timeout triggers.


property onuploadprogress [1/2]

QJSValue QFieldXmlHttpRequest::onuploadprogress;

Called during upload progress as (sentBytes, totalBytes).


property readyState [1/2]

ReadyState QFieldXmlHttpRequest::readyState;

The current request state aligned with browser XMLHttpRequest readyState values.


property response [1/2]

QVariant QFieldXmlHttpRequest::response;

The parsed response value when the response is JSON, otherwise this equals responseText.


property responseText [1/2]

QString QFieldXmlHttpRequest::responseText;

The response body decoded as UTF-8 text.


property responseType [1/2]

QString QFieldXmlHttpRequest::responseType;

The response Content-Type value as provided by the server.


property responseUrl [1/2]

QUrl QFieldXmlHttpRequest::responseUrl;

The final response URL. This may differ from the requested URL when redirects were followed.


property status [1/2]

int QFieldXmlHttpRequest::status;

The HTTP status code of the final response. For network-level failures, this is 0.


property statusText [1/2]

QString QFieldXmlHttpRequest::statusText;

The HTTP reason phrase of the final response when available.


property timeout [1/2]

int QFieldXmlHttpRequest::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 QFieldXmlHttpRequest::readyStateChanged;

The current request state aligned with browser XMLHttpRequest readyState values.


signal responseChanged

Emitted whenever response-related properties change.

void QFieldXmlHttpRequest::responseChanged;

signal timeoutChanged

void QFieldXmlHttpRequest::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 QFieldXmlHttpRequest

explicit QFieldXmlHttpRequest::QFieldXmlHttpRequest (
    QObject * parent=nullptr
) 

function abort

Q_INVOKABLE void QFieldXmlHttpRequest::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 QFieldXmlHttpRequest::getAllResponseHeaders () const

Returns all response headers in HTTP-style format. This is available only after the request finishes.


function getResponseHeader

Q_INVOKABLE QString QFieldXmlHttpRequest::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 QFieldXmlHttpRequest::onaborted () const

Called when the request is aborted.


function ondownloadprogress [2/2]

inline QJSValue QFieldXmlHttpRequest::ondownloadprogress () const

Called during download progress as (receivedBytes, totalBytes).


function onerror [2/2]

inline QJSValue QFieldXmlHttpRequest::onerror () const

Called for network-level failures as (errorCode, errorMessage).


function onload [2/2]

inline QJSValue QFieldXmlHttpRequest::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 QFieldXmlHttpRequest::onloadend () const

Called at the end of the request lifecycle for success, error, abort, or timeout.


function onreadystatechange [2/2]

inline QJSValue QFieldXmlHttpRequest::onreadystatechange () const

Called whenever readyState changes.


function onredirected [2/2]

inline QJSValue QFieldXmlHttpRequest::onredirected () const

Called when a redirect is reported by Qt as (urlString).


function ontimeout [2/2]

inline QJSValue QFieldXmlHttpRequest::ontimeout () const

Called when the timeout triggers.


function onuploadprogress [2/2]

inline QJSValue QFieldXmlHttpRequest::onuploadprogress () const

Called during upload progress as (sentBytes, totalBytes).


function open

Q_INVOKABLE void QFieldXmlHttpRequest::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 QFieldXmlHttpRequest::readyState () const

The current request state aligned with browser XMLHttpRequest readyState values.


function response [2/2]

inline QVariant QFieldXmlHttpRequest::response () const

The parsed response value when the response is JSON, otherwise this equals responseText.


function responseText [2/2]

inline QString QFieldXmlHttpRequest::responseText () const

The response body decoded as UTF-8 text.


function responseType [2/2]

inline QString QFieldXmlHttpRequest::responseType () const

The response Content-Type value as provided by the server.


function responseUrl [2/2]

inline QUrl QFieldXmlHttpRequest::responseUrl () const

The final response URL. This may differ from the requested URL when redirects were followed.


function send

Q_INVOKABLE void QFieldXmlHttpRequest::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 QFieldXmlHttpRequest::setOnaborted (
    const QJSValue & callback
) 

Called when the request is aborted.


function setOndownloadprogress

inline void QFieldXmlHttpRequest::setOndownloadprogress (
    const QJSValue & callback
) 

Called during download progress as (receivedBytes, totalBytes).


function setOnerror

inline void QFieldXmlHttpRequest::setOnerror (
    const QJSValue & callback
) 

Called for network-level failures as (errorCode, errorMessage).


function setOnload

inline void QFieldXmlHttpRequest::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 QFieldXmlHttpRequest::setOnloadend (
    const QJSValue & callback
) 

Called at the end of the request lifecycle for success, error, abort, or timeout.


function setOnreadystatechange

inline void QFieldXmlHttpRequest::setOnreadystatechange (
    const QJSValue & callback
) 

Called whenever readyState changes.


function setOnredirected

inline void QFieldXmlHttpRequest::setOnredirected (
    const QJSValue & callback
) 

Called when a redirect is reported by Qt as (urlString).


function setOntimeout

inline void QFieldXmlHttpRequest::setOntimeout (
    const QJSValue & callback
) 

Called when the timeout triggers.


function setOnuploadprogress

inline void QFieldXmlHttpRequest::setOnuploadprogress (
    const QJSValue & callback
) 

Called during upload progress as (sentBytes, totalBytes).


function setRequestHeader

Q_INVOKABLE void QFieldXmlHttpRequest::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 QFieldXmlHttpRequest::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 QFieldXmlHttpRequest::status () const

The HTTP status code of the final response. For network-level failures, this is 0.


function statusText [2/2]

inline QString QFieldXmlHttpRequest::statusText () const

The HTTP reason phrase of the final response when available.


function timeout [2/2]

inline int QFieldXmlHttpRequest::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 ~QFieldXmlHttpRequest

QFieldXmlHttpRequest::~QFieldXmlHttpRequest () override

Public Static Functions Documentation

function newRequest

static Q_INVOKABLE QFieldXmlHttpRequest * QFieldXmlHttpRequest::newRequest (
    QObject * parent=nullptr
) 

Creates a new request instance.



The documentation for this class was generated from the following file src/core/qfieldxmlhttprequest.h