- class QRestAccessManager#
The
QRestAccessManager
is a convenience wrapper forQNetworkAccessManager
. More…New 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.
QRestAccessManager
is 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
QNetworkAccessManager
directly.QRestAccessManager
does not take ownership of the wrappedQNetworkAccessManager
.QRestAccessManager
and relatedQRestReply
classes can only be used in the thread they live in. For further information see QObject thread affinity documentation.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.
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
X
X
X
QIODevice
X
X
X
X
X
*) QJsonDocument is sent in QJsonDocument::Compact format, and the
Content-Type
header is set toapplication/json
if theContent-Type
header was not set**) QVariantMap is converted to and treated as a QJsonObject
- __init__(manager[, parent=None])#
- Parameters:
manager –
QNetworkAccessManager
parent –
QObject
Constructs a
QRestAccessManager
object and setsparent
as the parent object, andmanager
as the underlyingQNetworkAccessManager
which is used for communication.See also
- deleteResource(request, context, slot)#
- Parameters:
request –
QNetworkRequest
context –
QObject
slot –
PyCallable
- Return type:
- deleteResource(request)
- Parameters:
request –
QNetworkRequest
- Return type:
- get(request, data, context, slot)#
- Parameters:
request –
QNetworkRequest
data –
QByteArray
context –
QObject
slot –
PyCallable
- Return type:
- get(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QIODevice
context –
QObject
slot –
PyCallable
- Return type:
- get(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QJsonDocument
context –
QObject
slot –
PyCallable
- Return type:
- get(request, context, slot)
- Parameters:
request –
QNetworkRequest
context –
QObject
slot –
PyCallable
- Return type:
- get(request)
- Parameters:
request –
QNetworkRequest
- Return type:
- get(request, data)
- Parameters:
request –
QNetworkRequest
data –
QIODevice
- Return type:
- get(request, data)
- Parameters:
request –
QNetworkRequest
data –
QByteArray
- Return type:
- get(request, data)
- Parameters:
request –
QNetworkRequest
data –
QJsonDocument
- Return type:
- head(request, context, slot)#
- Parameters:
request –
QNetworkRequest
context –
QObject
slot –
PyCallable
- Return type:
- head(request)
- Parameters:
request –
QNetworkRequest
- Return type:
- networkAccessManager()#
- Return type:
Returns the underlying
QNetworkAccessManager
instance.See also
- patch(request, data, context, slot)#
- Parameters:
request –
QNetworkRequest
data –
QByteArray
context –
QObject
slot –
PyCallable
- Return type:
- patch(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QIODevice
context –
QObject
slot –
PyCallable
- Return type:
- patch(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QJsonDocument
context –
QObject
slot –
PyCallable
- Return type:
- patch(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data – Dictionary with keys of type .QString and values of type QVariant.
context –
QObject
slot –
PyCallable
- Return type:
- patch(request, data)
- Parameters:
request –
QNetworkRequest
data –
QIODevice
- Return type:
- patch(request, data)
- Parameters:
request –
QNetworkRequest
data –
QByteArray
- Return type:
- patch(request, data)
- Parameters:
request –
QNetworkRequest
data –
QJsonDocument
- Return type:
- patch(request, data)
- Parameters:
request –
QNetworkRequest
data – Dictionary with keys of type .QString and values of type QVariant.
- Return type:
- post(request, data)#
- Parameters:
request –
QNetworkRequest
data – Dictionary with keys of type .QString and values of type QVariant.
- Return type:
- post(request, data)
- Parameters:
request –
QNetworkRequest
data –
QJsonDocument
- Return type:
- post(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QIODevice
context –
QObject
slot –
PyCallable
- Return type:
- post(request, data)
- Parameters:
request –
QNetworkRequest
data –
QByteArray
- Return type:
- post(request, data)
- Parameters:
request –
QNetworkRequest
data –
QIODevice
- Return type:
- post(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data – Dictionary with keys of type .QString and values of type QVariant.
context –
QObject
slot –
PyCallable
- Return type:
- post(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QJsonDocument
context –
QObject
slot –
PyCallable
- Return type:
- post(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QHttpMultiPart
context –
QObject
slot –
PyCallable
- Return type:
- post(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QByteArray
context –
QObject
slot –
PyCallable
- Return type:
- post(request, data)
- Parameters:
request –
QNetworkRequest
data –
QHttpMultiPart
- Return type:
- put(request, data)#
- Parameters:
request –
QNetworkRequest
data –
QHttpMultiPart
- Return type:
- put(request, data)
- Parameters:
request –
QNetworkRequest
data – Dictionary with keys of type .QString and values of type QVariant.
- Return type:
- put(request, data)
- Parameters:
request –
QNetworkRequest
data –
QJsonDocument
- Return type:
- put(request, data)
- Parameters:
request –
QNetworkRequest
data –
QByteArray
- Return type:
- put(request, data)
- Parameters:
request –
QNetworkRequest
data –
QIODevice
- Return type:
- put(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data – Dictionary with keys of type .QString and values of type QVariant.
context –
QObject
slot –
PyCallable
- Return type:
- put(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QJsonDocument
context –
QObject
slot –
PyCallable
- Return type:
- put(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QIODevice
context –
QObject
slot –
PyCallable
- Return type:
- put(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QHttpMultiPart
context –
QObject
slot –
PyCallable
- Return type:
- put(request, data, context, slot)
- Parameters:
request –
QNetworkRequest
data –
QByteArray
context –
QObject
slot –
PyCallable
- Return type:
- sendCustomRequest(request, method, data, context, slot)#
- Parameters:
request –
QNetworkRequest
method –
QByteArray
data –
QByteArray
context –
QObject
slot –
PyCallable
- Return type:
- sendCustomRequest(request, method, data, context, slot)
- Parameters:
request –
QNetworkRequest
method –
QByteArray
data –
QHttpMultiPart
context –
QObject
slot –
PyCallable
- Return type:
- sendCustomRequest(request, method, data, context, slot)
- Parameters:
request –
QNetworkRequest
method –
QByteArray
data –
QIODevice
context –
QObject
slot –
PyCallable
- Return type:
- sendCustomRequest(request, method, data)
- Parameters:
request –
QNetworkRequest
method –
QByteArray
data –
QHttpMultiPart
- Return type:
- sendCustomRequest(request, method, data)
- Parameters:
request –
QNetworkRequest
method –
QByteArray
data –
QIODevice
- Return type:
- sendCustomRequest(request, method, data)
- Parameters:
request –
QNetworkRequest
method –
QByteArray
data –
QByteArray
- Return type: