19#include "moc_qgsalgorithmhttprequest.cpp"
25#include <QNetworkRequest>
26#include <QDesktopServices>
28#include <QMimeDatabase>
32QString QgsHttpRequestAlgorithm::name()
const
34 return QStringLiteral(
"httprequest" );
37QString QgsHttpRequestAlgorithm::displayName()
const
39 return tr(
"HTTP(S) POST/GET request" );
42QString QgsHttpRequestAlgorithm::shortDescription()
const
44 return tr(
"Performs a HTTP(S) POST/GET request and returns the result code, error message and the data" );
47QStringList QgsHttpRequestAlgorithm::tags()
const
49 return tr(
"open,url,internet,url,fetch,get,post,request,https,http,download" ).split(
',' );
52QString QgsHttpRequestAlgorithm::group()
const
54 return tr(
"File tools" );
57QString QgsHttpRequestAlgorithm::groupId()
const
59 return QStringLiteral(
"filetools" );
62QString QgsHttpRequestAlgorithm::shortHelpString()
const
64 return tr(
"This algorithm performs a HTTP(S) POST/GET request and returns the HTTP status code and the reply data.\n"
65 "If an error occurs then the error code and the message will be returned.\n\n"
66 "Optionally, the result can be written to a file on disk.\n\n"
67 "By default the algorithm will warn on errors. Optionally, the algorithm can be set to treat HTTP errors as failures." );
70QgsHttpRequestAlgorithm *QgsHttpRequestAlgorithm::createInstance()
const
72 return new QgsHttpRequestAlgorithm();
75void QgsHttpRequestAlgorithm::initAlgorithm(
const QVariantMap & )
79 std::unique_ptr<QgsProcessingParameterEnum> methodParam = std::make_unique<QgsProcessingParameterEnum>(
80 QStringLiteral(
"METHOD" ),
81 QObject::tr(
"Method" ),
83 << QStringLiteral(
"GET" )
84 << QStringLiteral(
"POST" ),
88 methodParam->setHelp( QObject::tr(
"The HTTP method to use for the request" ) );
89 addParameter( methodParam.release() );
91 std::unique_ptr<QgsProcessingParameterString> dataParam = std::make_unique<QgsProcessingParameterString>(
92 QStringLiteral(
"DATA" ), tr(
"POST data" ), QVariant(),
false,
true
94 dataParam->setHelp( QObject::tr(
"The data to add in the body if the request is a POST" ) );
95 addParameter( dataParam.release() );
97 std::unique_ptr<QgsProcessingParameterFileDestination> outputFileParam = std::make_unique<QgsProcessingParameterFileDestination>(
98 QStringLiteral(
"OUTPUT" ), tr(
"File destination" ), QObject::tr(
"All files (*.*)" ), QVariant(),
true,
false
100 outputFileParam->setHelp( tr(
"The result can be written to a file instead of being returned as a string" ) );
101 addParameter( outputFileParam.release() );
103 std::unique_ptr<QgsProcessingParameterAuthConfig> authConfigParam = std::make_unique<QgsProcessingParameterAuthConfig>(
104 QStringLiteral(
"AUTH_CONFIG" ), tr(
"Authentication" ), QVariant(),
true
106 authConfigParam->setHelp( tr(
"An authentication configuration to pass" ) );
107 addParameter( authConfigParam.release() );
109 std::unique_ptr<QgsProcessingParameterBoolean> failureParam = std::make_unique<QgsProcessingParameterBoolean>(
110 QStringLiteral(
"FAIL_ON_ERROR" ), tr(
"Consider HTTP errors as failures" ),
false
112 failureParam->setHelp( tr(
"If set, the algorithm will fail on encountering a HTTP error" ) );
113 addParameter( failureParam.release() );
123 const QString url = parameterAsString( parameters, QStringLiteral(
"URL" ), context );
127 const QString data = parameterAsString( parameters, QStringLiteral(
"DATA" ), context );
128 const QString authCfg = parameterAsString( parameters, QStringLiteral(
"AUTH_CONFIG" ), context );
129 const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral(
"OUTPUT" ), context );
130 const bool failOnError = parameterAsBool( parameters, QStringLiteral(
"FAIL_ON_ERROR" ), context );
131 const QUrl qurl = QUrl::fromUserInput( url );
134 QNetworkRequest request( qurl );
139 switch ( httpMethod )
143 errorCode = blockingRequest.
get( request );
148 errorCode = blockingRequest.
post( request, data.toUtf8() );
154 const int statusCode = blockingRequest.
reply().
attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
155 QString errorMessage = QString();
156 QByteArray resultData = QByteArray();
160 feedback->
pushInfo( tr(
"Request succeeded with code %1" ).arg( statusCode ) );
163 if ( !outputFile.isEmpty() )
165 QFile tempFile( outputFile );
166 tempFile.open( QIODevice::WriteOnly );
167 tempFile.write( resultData );
170 feedback->
pushInfo( tr(
"Result data written to %1" ).arg( outputFile ) );
175 feedback->
pushInfo( tr(
"Request failed with code %1" ).arg( statusCode ) );
185 outputs.insert( QStringLiteral(
"STATUS_CODE" ), statusCode );
186 outputs.insert( QStringLiteral(
"ERROR_CODE" ), errorCode );
187 outputs.insert( QStringLiteral(
"ERROR_MESSAGE" ), errorMessage );
188 outputs.insert( QStringLiteral(
"RESULT_DATA" ), QString( resultData ) );
189 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
HttpMethod
Different methods of HTTP requests.
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
ErrorCode post(QNetworkRequest &request, QIODevice *data, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "post" operation on the specified request, using the given data.
void setAuthCfg(const QString &authCfg)
Sets the authentication config id which should be used during the request.
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr, RequestFlags requestFlags=QgsBlockingNetworkRequest::RequestFlags())
Performs a "get" operation on the specified request.
@ NoError
No error was encountered.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
QVariant attribute(QNetworkRequest::Attribute code) const
Returns the attribute associated with the code.
QString errorString() const
Returns the error text for the reply, or an empty string if no error was encountered.
QByteArray content() const
Returns the reply content.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
A numeric output for processing algorithms.
A string output for processing algorithms.
A string parameter for processing algorithms.