Class: QgsNetworkContentFetcherTask

Handles HTTP network content fetching in a background task.

Provides a simple method for fetching remote HTTP content in a QgsTask. Url redirects are automatically handled.

After constructing a QgsNetworkContentFetcherTask, callers should connect to the QgsNetworkContentFetcherTask.fetched signal. They can then safely access the network reply() from the connected slot without danger of the task being first removed by the QgsTaskManager.

Added in version 3.2.

Class Hierarchy

Inheritance diagram of qgis.core.QgsNetworkContentFetcherTask

Base classes

QgsTask

Abstract base class for long running background tasks.

QObject

Methods

contentAsString

Returns the fetched content as a string

contentDispositionFilename

Returns the associated filename from the reply's content disposition header, if present.

reply

Returns the network reply.

Signals

errorOccurred

Emitted when an error with code error occurred while processing the request errorMsg is a textual description of the error

fetched

Emitted when the network content has been fetched, regardless of whether the fetch was successful or not.

class qgis.core.QgsNetworkContentFetcherTask[source]

Bases: QgsTask

__init__(url: QUrl, authcfg: str | None = '', flags: QgsTask.Flags | QgsTask.Flag = QgsTask.CanCancel, description: str | None = '')

Constructor for a QgsNetworkContentFetcherTask which fetches the specified url.

Optionally, authentication configuration can be set via the authcfg argument.

Since QGIS 3.28 the optional description can be explicitly set. If not specified, a default task description will be generated.

Parameters:
  • url (QUrl)

  • authcfg (Optional[str] = '')

  • flags (Union[QgsTask.Flags, QgsTask.Flag] = QgsTask.CanCancel)

  • description (Optional[str] = '')

__init__(request: QNetworkRequest, authcfg: str | None = '', flags: QgsTask.Flags | QgsTask.Flag = QgsTask.CanCancel, description: str | None = '')

Constructor for a QgsNetworkContentFetcherTask which fetches the specified network request.

Optionally, authentication configuration can be set via the authcfg argument.

Since QGIS 3.28 the optional description can be explicitly set. If not specified, a default task description will be generated.

Parameters:
  • request (QNetworkRequest)

  • authcfg (Optional[str] = '')

  • flags (Union[QgsTask.Flags, QgsTask.Flag] = QgsTask.CanCancel)

  • description (Optional[str] = '')

contentAsString(self) str[source]

Returns the fetched content as a string

Warning

This should only be accessed from a slot connected directly to the QgsNetworkContentFetcherTask.fetched() signal.

Added in version 3.10.

Return type:

str

contentDispositionFilename(self) str[source]

Returns the associated filename from the reply’s content disposition header, if present.

Added in version 3.28.

Return type:

str

signal errorOccurred(code: QNetworkReply.NetworkError, errorMsg: str)[source]

Emitted when an error with code error occurred while processing the request errorMsg is a textual description of the error

Added in version 3.22.

Parameters:
  • code (QNetworkReply.NetworkError)

  • errorMsg (str)

signal fetched[source]

Emitted when the network content has been fetched, regardless of whether the fetch was successful or not.

Users of QgsNetworkContentFetcherTask should connect to this signal, and from the associated slot they can then safely access the network reply() without danger of the task being first removed by the QgsTaskManager.

reply(self) QNetworkReply | None[source]

Returns the network reply. Ownership is not transferred.

May return None if the request has not yet completed.

Warning

This should only be accessed from a slot connected directly to the QgsNetworkContentFetcherTask.fetched() signal.

Return type:

Optional[QNetworkReply]