Class: QgsBlockingNetworkRequest

A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy and authentication settings.

This class should be used whenever a blocking network request is required. Unlike implementations which rely on QApplication.processEvents() or creation of a QEventLoop, this class is completely thread safe and can be used on either the main thread or background threads without issue.

Redirects are automatically handled by the class.

After completion of a request, the reply content should be retrieved by calling getReplyContent(). This method returns a QgsNetworkReplyContent container, which is safe and cheap to copy and pass between threads without issue.

Added in version 3.6.

Class Hierarchy

Inheritance diagram of qgis.core.QgsBlockingNetworkRequest

Base classes

QObject

Enums

RequestFlag

Request flags

Methods

abort

Aborts the network request immediately.

authCfg

Returns the authentication config id which will be used during the request.

deleteResource

Performs a "delete" operation on the specified request.

errorMessage

Returns the error message string, after a get(), post(), head() or put() request has been made.

get

Performs a "get" operation on the specified request.

head

Performs a "head" operation on the specified request.

post

Performs a "post" operation on the specified request, using the given data.

put

Performs a "put" operation on the specified request, using the given data.

reply

Returns the content of the network reply, after a get(), post(), head() or put() request has been made.

setAuthCfg

Sets the authentication config id which should be used during the request.

Signals

downloadFinished

Emitted once a request has finished downloading.

downloadProgress

Emitted when when data arrives during a request.

finished

Emitted once a request has finished.

uploadProgress

Emitted when when data are sent during a request.

Attributes

NetworkError

NoError

ServerExceptionError

TimeoutError

class qgis.core.QgsBlockingNetworkRequest[source]

Bases: QObject

__init__()

Constructor for QgsBlockingNetworkRequest

class ErrorCode

Bases: int

NetworkError = 1
NoError = 0
class RequestFlag(*values)

Bases: IntEnum

Request flags

Added in version 3.40.

  • EmptyResponseIsValid: Do not generate an error if getting an empty response (e.g. HTTP 204)

EmptyResponseIsValid = 1
class RequestFlags
class RequestFlags(f: QgsBlockingNetworkRequest.RequestFlags | QgsBlockingNetworkRequest.RequestFlag)
class RequestFlags(a0: QgsBlockingNetworkRequest.RequestFlags)

Bases: object

ServerExceptionError = 3
TimeoutError = 2
abort(self)[source]

Aborts the network request immediately.

authCfg(self) str[source]

Returns the authentication config id which will be used during the request.

See also

setAuthCfg()

Return type:

str

deleteResource(self, request: QNetworkRequest, feedback: QgsFeedback | None = None) QgsBlockingNetworkRequest.ErrorCode[source]

Performs a “delete” operation on the specified request.

If an authCfg() has been set, then any authentication configuration required will automatically be applied to request. There is no need to manually apply the authentication to the request prior to calling this method.

The optional feedback argument can be used to abort ongoing requests.

The method will return NoError if the get operation was successful. The contents of the reply can be retrieved by calling reply().

If an error was encountered then a specific ErrorCode will be returned, and a detailed error message can be retrieved by calling errorMessage().

Added in version 3.18.

Parameters:
  • request (QNetworkRequest)

  • feedback (Optional[QgsFeedback] = None)

Return type:

QgsBlockingNetworkRequest.ErrorCode

signal downloadFinished[source]

Emitted once a request has finished downloading.

Deprecated since version 3.40: Use the finished() signal instead.

signal downloadProgress(bytesReceived: int, bytesTotal: int)[source]

Emitted when when data arrives during a request.

Parameters:
  • bytesReceived (int)

  • bytesTotal (int)

errorMessage(self) str[source]

Returns the error message string, after a get(), post(), head() or put() request has been made.

Return type:

str

signal finished[source]

Emitted once a request has finished.

get(self, request: QNetworkRequest, forceRefresh: bool = False, feedback: QgsFeedback | None = None, requestFlags: QgsBlockingNetworkRequest.RequestFlags | QgsBlockingNetworkRequest.RequestFlag = QgsBlockingNetworkRequest.RequestFlags()) QgsBlockingNetworkRequest.ErrorCode[source]

Performs a “get” operation on the specified request.

If forceRefresh is False then previously cached replies may be used for the request. If it is set to True then a new query is always performed.

If an authCfg() has been set, then any authentication configuration required will automatically be applied to request. There is no need to manually apply the authentication to the request prior to calling this method.

The optional feedback argument can be used to abort ongoing requests.

The optional requestFlags argument can be used to modify the behavior (added in QGIS 3.40).

The method will return NoError if the get operation was successful. The contents of the reply can be retrieved by calling reply().

If an error was encountered then a specific ErrorCode will be returned, and a detailed error message can be retrieved by calling errorMessage().

See also

post()

Parameters:
Return type:

QgsBlockingNetworkRequest.ErrorCode

head(self, request: QNetworkRequest, forceRefresh: bool = False, feedback: QgsFeedback | None = None) QgsBlockingNetworkRequest.ErrorCode[source]

Performs a “head” operation on the specified request.

If forceRefresh is False then previously cached replies may be used for the request. If it is set to True then a new query is always performed.

If an authCfg() has been set, then any authentication configuration required will automatically be applied to request. There is no need to manually apply the authentication to the request prior to calling this method.

The optional feedback argument can be used to abort ongoing requests.

The method will return NoError if the get operation was successful. The contents of the reply can be retrieved by calling reply().

If an error was encountered then a specific ErrorCode will be returned, and a detailed error message can be retrieved by calling errorMessage().

Added in version 3.18.

Parameters:
  • request (QNetworkRequest)

  • forceRefresh (bool = False)

  • feedback (Optional[QgsFeedback] = None)

Return type:

QgsBlockingNetworkRequest.ErrorCode

post(self, request: QNetworkRequest, data: QIODevice | None, forceRefresh: bool = False, feedback: QgsFeedback | None = None) QgsBlockingNetworkRequest.ErrorCode[source]

Performs a “post” operation on the specified request, using the given data.

If forceRefresh is False then previously cached replies may be used for the request. If it is set to True then a new query is always performed.

If an authCfg() has been set, then any authentication configuration required will automatically be applied to request. There is no need to manually apply the authentication to the request prior to calling this method.

The optional feedback argument can be used to abort ongoing requests.

The method will return NoError if the get operation was successful. The contents of the reply can be retrieved by calling reply().

If an error was encountered then a specific ErrorCode will be returned, and a detailed error message can be retrieved by calling errorMessage().

See also

get()

Added in version 3.22.

Parameters:
  • request (QNetworkRequest)

  • data (Optional[QIODevice])

  • forceRefresh (bool = False)

  • feedback (Optional[QgsFeedback] = None)

Return type:

QgsBlockingNetworkRequest.ErrorCode

post(self, request: QNetworkRequest, data: QByteArray | bytes | bytearray, forceRefresh: bool = False, feedback: QgsFeedback | None = None) QgsBlockingNetworkRequest.ErrorCode[source]

This is an overloaded function.

Performs a “post” operation on the specified request, using the given data.

Parameters:
  • request (QNetworkRequest)

  • data (Union[QByteArray, bytes, bytearray])

  • forceRefresh (bool = False)

  • feedback (Optional[QgsFeedback] = None)

Return type:

QgsBlockingNetworkRequest.ErrorCode

put(self, request: QNetworkRequest, data: QIODevice | None, feedback: QgsFeedback | None = None) QgsBlockingNetworkRequest.ErrorCode[source]

Performs a “put” operation on the specified request, using the given data.

If an authCfg() has been set, then any authentication configuration required will automatically be applied to request. There is no need to manually apply the authentication to the request prior to calling this method.

The optional feedback argument can be used to abort ongoing requests.

The method will return NoError if the get operation was successful. The contents of the reply can be retrieved by calling reply().

If an error was encountered then a specific ErrorCode will be returned, and a detailed error message can be retrieved by calling errorMessage().

Added in version 3.22.

Parameters:
  • request (QNetworkRequest)

  • data (Optional[QIODevice])

  • feedback (Optional[QgsFeedback] = None)

Return type:

QgsBlockingNetworkRequest.ErrorCode

put(self, request: QNetworkRequest, data: QByteArray | bytes | bytearray, feedback: QgsFeedback | None = None) QgsBlockingNetworkRequest.ErrorCode[source]

This is an overloaded function.

Performs a “put” operation on the specified request, using the given data.

Added in version 3.18.

Parameters:
  • request (QNetworkRequest)

  • data (Union[QByteArray, bytes, bytearray])

  • feedback (Optional[QgsFeedback] = None)

Return type:

QgsBlockingNetworkRequest.ErrorCode

reply(self) QgsNetworkReplyContent[source]

Returns the content of the network reply, after a get(), post(), head() or put() request has been made.

Return type:

QgsNetworkReplyContent

setAuthCfg(self, authCfg: str | None)[source]

Sets the authentication config id which should be used during the request.

See also

authCfg()

Parameters:

authCfg (Optional[str])

signal uploadProgress(bytesReceived: int, bytesTotal: int)[source]

Emitted when when data are sent during a request.

Added in version 3.22.

Parameters:
  • bytesReceived (int)

  • bytesTotal (int)