24#include <QDesktopServices>
25#include <QMimeDatabase>
26#include <QNetworkRequest>
31#include "moc_qgsalgorithmhttprequest.cpp"
33using namespace Qt::StringLiterals;
37QString QgsHttpRequestAlgorithm::name()
const
39 return u
"httprequest"_s;
42QString QgsHttpRequestAlgorithm::displayName()
const
44 return tr(
"HTTP(S) POST/GET request" );
47QString QgsHttpRequestAlgorithm::shortDescription()
const
49 return tr(
"Performs a HTTP(S) POST/GET request and returns the result code, error message and the data." );
52QStringList QgsHttpRequestAlgorithm::tags()
const
54 return tr(
"open,url,internet,url,fetch,get,post,request,https,http,download" ).split(
',' );
57QString QgsHttpRequestAlgorithm::group()
const
59 return tr(
"File tools" );
62QString QgsHttpRequestAlgorithm::groupId()
const
64 return u
"filetools"_s;
67QString QgsHttpRequestAlgorithm::shortHelpString()
const
70 "This algorithm performs a HTTP(S) POST/GET request and returns the HTTP status code and the reply data.\n"
71 "If an error occurs then the error code and the message will be returned.\n\n"
72 "Optionally, the result can be written to a file on disk.\n\n"
73 "By default the algorithm will warn on errors. Optionally, the algorithm can be set to treat HTTP errors as failures."
77QgsHttpRequestAlgorithm *QgsHttpRequestAlgorithm::createInstance()
const
79 return new QgsHttpRequestAlgorithm();
82void QgsHttpRequestAlgorithm::initAlgorithm(
const QVariantMap & )
86 auto methodParam = std::make_unique<QgsProcessingParameterEnum>( u
"METHOD"_s, QObject::tr(
"Method" ), QStringList() << u
"GET"_s << u
"POST"_s,
false, 0 );
87 methodParam->setHelp( QObject::tr(
"The HTTP method to use for the request" ) );
88 addParameter( methodParam.release() );
90 auto dataParam = std::make_unique<QgsProcessingParameterString>( u
"DATA"_s, tr(
"POST data" ), QVariant(),
false,
true );
91 dataParam->setHelp( QObject::tr(
"The data to add in the body if the request is a POST" ) );
92 addParameter( dataParam.release() );
94 auto outputFileParam = std::make_unique<QgsProcessingParameterFileDestination>( u
"OUTPUT"_s, tr(
"File destination" ), QObject::tr(
"All files (*.*)" ), QVariant(),
true,
false );
95 outputFileParam->setHelp( tr(
"The result can be written to a file instead of being returned as a string" ) );
96 addParameter( outputFileParam.release() );
98 auto authConfigParam = std::make_unique<QgsProcessingParameterAuthConfig>( u
"AUTH_CONFIG"_s, tr(
"Authentication" ), QVariant(),
true );
99 authConfigParam->setHelp( tr(
"An authentication configuration to pass" ) );
100 addParameter( authConfigParam.release() );
102 auto failureParam = std::make_unique<QgsProcessingParameterBoolean>( u
"FAIL_ON_ERROR"_s, tr(
"Consider HTTP errors as failures" ),
false );
103 failureParam->setHelp( tr(
"If set, the algorithm will fail on encountering a HTTP error" ) );
104 addParameter( failureParam.release() );
114 const QString url = parameterAsString( parameters, u
"URL"_s, context );
118 const QString data = parameterAsString( parameters, u
"DATA"_s, context );
119 const QString authCfg = parameterAsString( parameters, u
"AUTH_CONFIG"_s, context );
120 const QString outputFile = parameterAsFileOutput( parameters, u
"OUTPUT"_s, context );
121 const bool failOnError = parameterAsBool( parameters, u
"FAIL_ON_ERROR"_s, context );
122 const QUrl qurl = QUrl::fromUserInput( url );
125 QNetworkRequest request( qurl );
130 switch ( httpMethod )
134 errorCode = blockingRequest.
get( request );
139 errorCode = blockingRequest.
post( request, data.toUtf8() );
150 const int statusCode = blockingRequest.
reply().
attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
151 QString errorMessage = QString();
152 QByteArray resultData = QByteArray();
156 feedback->
pushInfo( tr(
"Request succeeded with code %1" ).arg( statusCode ) );
159 if ( !outputFile.isEmpty() )
161 QFile tempFile( outputFile );
162 if ( !tempFile.open( QIODevice::WriteOnly ) )
166 tempFile.write( resultData );
169 feedback->
pushInfo( tr(
"Result data written to %1" ).arg( outputFile ) );
174 feedback->
pushInfo( tr(
"Request failed with code %1" ).arg( statusCode ) );
184 outputs.insert( u
"STATUS_CODE"_s, statusCode );
185 outputs.insert( u
"ERROR_CODE"_s, errorCode );
186 outputs.insert( u
"ERROR_MESSAGE"_s, errorMessage );
187 outputs.insert( u
"RESULT_DATA"_s, QString( resultData ) );
188 outputs.insert( u
"OUTPUT"_s, 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.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.