class QRestAccessManager

The QRestAccessManager is a convenience wrapper for QNetworkAccessManager . More

Inheritance diagram of PySide6.QtNetwork.QRestAccessManager

Added in version 6.7.

Synopsis

Methods

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

QRestAccessManager is a convenience wrapper on top of QNetworkAccessManager . It amends datatypes and HTTP methods that are useful for typical RESTful client applications.

The usual Qt networking features are accessible by configuring the wrapped QNetworkAccessManager directly. QRestAccessManager does not take ownership of the wrapped QNetworkAccessManager .

QRestAccessManager and related QRestReply classes can only be used in the thread they live in. See QObject thread affinity for more information.

Issuing Network Requests and Handling Replies

Network requests are initiated with a function call corresponding to the desired HTTP method, such as get() and post().

Using Signals and Slots

The function returns a QNetworkReply * object, whose signals can be used to follow up on the completion of the request in a traditional Qt-signals-and-slots way.

Here’s an example of how you could send a GET request and handle the response:

reply = manager.get(request)
reply.finished.connect(this, [reply]() {
    # The reply may be wrapped in the finish handler:
    restReply = QRestReply(reply)
    if restReply.isSuccess():
        # ...
})

Using Callbacks and Context Objects

The functions also take a context object of QObject (subclass) type and a callback function as parameters. The callback takes one QRestReply & as a parameter. The callback can be any callable, including a pointer-to-member-function.

These callbacks are invoked when the reply has finished processing (also in the case the processing finished due to an error).

The context object can be nullptr, although, generally speaking, this is discouraged. Using a valid context object ensures that if the context object is destroyed during request processing, the callback will not be called. Stray callbacks which access a destroyed context is a source of application misbehavior.

Here’s an example of how you could send a GET request and check the response:

# With lambda
manager.get(request, self, [self](QRestReply reply) {
    if reply.isSuccess():
        # ...

})
# With member function
manager->get(request, self.handleFinished)

Many of the functions take in data for sending to a server. The data is supplied as the second parameter after the request.

Here’s an example of how you could send a POST request and check the response:

myJson = QJsonDocument()
# ...
manager.post(request, myJson, self, [self](QRestReply reply) {
    if not reply.isSuccess():
        # ...

    if std.optional json = reply.readJson():
        # use *json

})

The provided QRestReply & is valid only while the callback executes. If you need it for longer, you can either move it to another QRestReply , or construct a new one and initialize it with the QNetworkReply (see networkReply() ).

Supported data types

The following table summarizes the methods and the supported data types. X means support.

Data type

get()

post()

put()

head()

patch()

deleteResource()

sendCustomRequest()

No data

X

X

X

QByteArray

X

X

X

X

X

QJsonDocument *)

X

X

X

X

QVariantMap **)

X

X

X

QHttpMultiPart

X

X

X

QIODevice

X

X

X

X

X

*) QJsonDocument is sent in QJsonDocument::Compact format, and the Content-Type header is set to application/json if the Content-Type header was not set

**) QVariantMap is converted to and treated as a QJsonObject

__init__(manager[, parent=None])
Parameters:

Constructs a QRestAccessManager object and sets parent as the parent object, and manager as the underlying QNetworkAccessManager which is used for communication.

deleteResource(request)
Parameters:

requestQNetworkRequest

Return type:

QNetworkReply

deleteResource(request, context, slot)
Parameters:
Return type:

QNetworkReply

get(request)
Parameters:

requestQNetworkRequest

Return type:

QNetworkReply

get(request, data)
Parameters:
Return type:

QNetworkReply

get(request, data)
Parameters:
Return type:

QNetworkReply

get(request, data)
Parameters:
Return type:

QNetworkReply

get(request, context, slot)
Parameters:
Return type:

QNetworkReply

get(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

get(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

get(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

head(request)
Parameters:

requestQNetworkRequest

Return type:

QNetworkReply

head(request, context, slot)
Parameters:
Return type:

QNetworkReply

networkAccessManager()
Return type:

QNetworkAccessManager

Returns the underlying QNetworkAccessManager instance.

patch(request, data)
Parameters:
Return type:

QNetworkReply

patch(request, data)
Parameters:
Return type:

QNetworkReply

patch(request, data)
Parameters:
Return type:

QNetworkReply

patch(request, data)
Parameters:
  • requestQNetworkRequest

  • data – Dictionary with keys of type .QString and values of type QVariant.

Return type:

QNetworkReply

patch(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

patch(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

patch(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

patch(request, data, context, slot)
Parameters:
  • requestQNetworkRequest

  • data – Dictionary with keys of type .QString and values of type QVariant.

  • contextQObject

  • slotPyCallable

Return type:

QNetworkReply

post(request, data)
Parameters:
Return type:

QNetworkReply

post(request, data)
Parameters:
Return type:

QNetworkReply

post(request, data)
Parameters:
Return type:

QNetworkReply

post(request, data)
Parameters:
Return type:

QNetworkReply

post(request, data)
Parameters:
  • requestQNetworkRequest

  • data – Dictionary with keys of type .QString and values of type QVariant.

Return type:

QNetworkReply

post(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

post(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

post(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

post(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

post(request, data, context, slot)
Parameters:
  • requestQNetworkRequest

  • data – Dictionary with keys of type .QString and values of type QVariant.

  • contextQObject

  • slotPyCallable

Return type:

QNetworkReply

put(request, data)
Parameters:
Return type:

QNetworkReply

put(request, data)
Parameters:
Return type:

QNetworkReply

put(request, data)
Parameters:
Return type:

QNetworkReply

put(request, data)
Parameters:
Return type:

QNetworkReply

put(request, data)
Parameters:
  • requestQNetworkRequest

  • data – Dictionary with keys of type .QString and values of type QVariant.

Return type:

QNetworkReply

put(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

put(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

put(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

put(request, data, context, slot)
Parameters:
Return type:

QNetworkReply

put(request, data, context, slot)
Parameters:
  • requestQNetworkRequest

  • data – Dictionary with keys of type .QString and values of type QVariant.

  • contextQObject

  • slotPyCallable

Return type:

QNetworkReply

sendCustomRequest(request, method, data)
Parameters:
Return type:

QNetworkReply

sendCustomRequest(request, method, data)
Parameters:
Return type:

QNetworkReply

sendCustomRequest(request, method, data)
Parameters:
Return type:

QNetworkReply

sendCustomRequest(request, method, data, context, slot)
Parameters:
Return type:

QNetworkReply

sendCustomRequest(request, method, data, context, slot)
Parameters:
Return type:

QNetworkReply

sendCustomRequest(request, method, data, context, slot)
Parameters:
Return type:

QNetworkReply