PySide6.QtNetwork.QRestAccessManager¶
- class QRestAccessManager¶
The
QRestAccessManageris a convenience wrapper forQNetworkAccessManager. More…Added in version 6.7.
Synopsis¶
Methods¶
def
__init__()def
deleteResource()def
get()def
head()def
patch()def
post()def
put()
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.
QRestAccessManageris a convenience wrapper on top ofQNetworkAccessManager. 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
QNetworkAccessManagerdirectly.QRestAccessManagerdoes not take ownership of the wrappedQNetworkAccessManager.QRestAccessManagerand relatedQRestReplyclasses 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()andpost().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 anotherQRestReply, or construct a new one and initialize it with theQNetworkReply(seenetworkReply()).Supported data types¶
The following table summarizes the methods and the supported data types.
Xmeans 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
X
X
X
QIODevice
X
X
X
X
X
*) QJsonDocument is sent in QJsonDocument::Compact format, and the
Content-Typeheader is set toapplication/jsonif theContent-Typeheader was not set**) QVariantMap is converted to and treated as a QJsonObject
- __init__(manager[, parent=None])¶
- Parameters:
manager –
QNetworkAccessManagerparent –
QObject
Constructs a
QRestAccessManagerobject and setsparentas the parent object, andmanageras the underlyingQNetworkAccessManagerwhich is used for communication.See also
- deleteResource(request)¶
- Parameters:
request –
QNetworkRequest- Return type:
- deleteResource(request, context, slot)
- Parameters:
request –
QNetworkRequestcontext –
QObjectslot –
PyCallable
- Return type:
- get(request)¶
- Parameters:
request –
QNetworkRequest- Return type:
- get(request, data)
- Parameters:
request –
QNetworkRequestdata –
QIODevice
- Return type:
- get(request, data)
- Parameters:
request –
QNetworkRequestdata –
QByteArray
- Return type:
- get(request, data)
- Parameters:
request –
QNetworkRequestdata –
QJsonDocument
- Return type:
- get(request, context, slot)
- Parameters:
request –
QNetworkRequestcontext –
QObjectslot –
PyCallable
- Return type:
- get(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QByteArraycontext –
QObjectslot –
PyCallable
- Return type:
- get(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QIODevicecontext –
QObjectslot –
PyCallable
- Return type:
- get(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QJsonDocumentcontext –
QObjectslot –
PyCallable
- Return type:
- head(request)¶
- Parameters:
request –
QNetworkRequest- Return type:
- head(request, context, slot)
- Parameters:
request –
QNetworkRequestcontext –
QObjectslot –
PyCallable
- Return type:
- networkAccessManager()¶
- Return type:
Returns the underlying
QNetworkAccessManagerinstance.See also
- patch(request, data)¶
- Parameters:
request –
QNetworkRequestdata –
QIODevice
- Return type:
- patch(request, data)
- Parameters:
request –
QNetworkRequestdata –
QByteArray
- Return type:
- patch(request, data)
- Parameters:
request –
QNetworkRequestdata –
QJsonDocument
- Return type:
- patch(request, data)
- Parameters:
request –
QNetworkRequestdata – Dictionary with keys of type .QString and values of type QVariant.
- Return type:
- patch(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QByteArraycontext –
QObjectslot –
PyCallable
- Return type:
- patch(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QIODevicecontext –
QObjectslot –
PyCallable
- Return type:
- patch(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QJsonDocumentcontext –
QObjectslot –
PyCallable
- Return type:
- patch(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata – Dictionary with keys of type .QString and values of type QVariant.
context –
QObjectslot –
PyCallable
- Return type:
- post(request, data)¶
- Parameters:
request –
QNetworkRequestdata –
QHttpMultiPart
- Return type:
- post(request, data)
- Parameters:
request –
QNetworkRequestdata –
QIODevice
- Return type:
- post(request, data)
- Parameters:
request –
QNetworkRequestdata –
QByteArray
- Return type:
- post(request, data)
- Parameters:
request –
QNetworkRequestdata –
QJsonDocument
- Return type:
- post(request, data)
- Parameters:
request –
QNetworkRequestdata – Dictionary with keys of type .QString and values of type QVariant.
- Return type:
- post(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QByteArraycontext –
QObjectslot –
PyCallable
- Return type:
- post(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QHttpMultiPartcontext –
QObjectslot –
PyCallable
- Return type:
- post(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QIODevicecontext –
QObjectslot –
PyCallable
- Return type:
- post(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QJsonDocumentcontext –
QObjectslot –
PyCallable
- Return type:
- post(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata – Dictionary with keys of type .QString and values of type QVariant.
context –
QObjectslot –
PyCallable
- Return type:
- put(request, data)¶
- Parameters:
request –
QNetworkRequestdata –
QHttpMultiPart
- Return type:
- put(request, data)
- Parameters:
request –
QNetworkRequestdata –
QIODevice
- Return type:
- put(request, data)
- Parameters:
request –
QNetworkRequestdata –
QByteArray
- Return type:
- put(request, data)
- Parameters:
request –
QNetworkRequestdata –
QJsonDocument
- Return type:
- put(request, data)
- Parameters:
request –
QNetworkRequestdata – Dictionary with keys of type .QString and values of type QVariant.
- Return type:
- put(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QByteArraycontext –
QObjectslot –
PyCallable
- Return type:
- put(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QHttpMultiPartcontext –
QObjectslot –
PyCallable
- Return type:
- put(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QIODevicecontext –
QObjectslot –
PyCallable
- Return type:
- put(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata –
QJsonDocumentcontext –
QObjectslot –
PyCallable
- Return type:
- put(request, data, context, slot)
- Parameters:
request –
QNetworkRequestdata – Dictionary with keys of type .QString and values of type QVariant.
context –
QObjectslot –
PyCallable
- Return type:
- sendCustomRequest(request, method, data)¶
- Parameters:
request –
QNetworkRequestmethod –
QByteArraydata –
QHttpMultiPart
- Return type:
- sendCustomRequest(request, method, data)
- Parameters:
request –
QNetworkRequestmethod –
QByteArraydata –
QIODevice
- Return type:
- sendCustomRequest(request, method, data)
- Parameters:
request –
QNetworkRequestmethod –
QByteArraydata –
QByteArray
- Return type:
- sendCustomRequest(request, method, data, context, slot)
- Parameters:
request –
QNetworkRequestmethod –
QByteArraydata –
QByteArraycontext –
QObjectslot –
PyCallable
- Return type:
- sendCustomRequest(request, method, data, context, slot)
- Parameters:
request –
QNetworkRequestmethod –
QByteArraydata –
QHttpMultiPartcontext –
QObjectslot –
PyCallable
- Return type:
- sendCustomRequest(request, method, data, context, slot)
- Parameters:
request –
QNetworkRequestmethod –
QByteArraydata –
QIODevicecontext –
QObjectslot –
PyCallable
- Return type: