Question

I am trying to do a very simple REST client in QT however I am getting an "unknown error" in my Reply. Also when I do a packet capture, I see 0 packets getting sent to the server.

My code is:

#include <QCoreApplication>
#include <QtNetwork>
#include <QDebug>

int main(int argc, char *argv[])
{
 QCoreApplication a(argc, argv);
 QNetworkAccessManager *NETWORK = new QNetworkAccessManager();
 NETWORK->setNetworkAccessible(QNetworkAccessManager::Accessible);
 QNetworkRequest REQUEST;
 REQUEST.setUrl(QUrl("http://test.com/test/rest/BOB"));
 QNetworkReply *REPLY = NETWORK->get(REQUEST);
 qDebug() << REPLY->errorString();
 return 0;
 }

and my result is: unknown error.

The simple Application does compile without errors or warnings.

can anybody see any issues?

My expected output is "Hello BOB" and was successfully tested with a PHP client using Curl.

Was it helpful?

Solution

You seem to be misusing the QtNetwork API. You have at least the following issues ongoing:

  • You are including the whole QtNetwork module as opposed to the necessary classes.

  • You call the errorStrin() before the error() signal is emitted. This is not good, and what you see is expected because the default state is UnknownError as per documentation.

  • You are not connecting to the finished signal of the async API. The returned QNetworkReply is just a pointer that you can use later to access members, but it does not yet contain the reply that at that point.

  • You are instantiating a QCoreApplication, but not actually using it for running an event loop which is necessary for the async operation for getting the reply. You could always block, too, but that is not really recommended most of the time.

  • You are using a pointer for the QNetworkAccessManager here needlessly.

  • You are using macro'ish uppercase letters for a regular variable name.

You could write some code like below. It would be simpler to use lambda with C++11, but I am assuming you need to get it work with pre-such compilers.

main.cpp

#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QNetworkRequest>

#include <QCoreApplication>
#include <QDebug>
#include <QUrl>

class Download : public QObject
{
    Q_OBJECT
    public:
        explicit Download(QObject *parent = 0) : QObject (parent) {}
        ~Download() {}

    public slots:
        // void handleFinished() {}
        void handleFinished(QNetworkReply* networkReply) { qDebug() << "Reply:" << networkReply->readAll(); QCoreApplication::quit(); }
        void handleError(QNetworkReply::NetworkError code) { qDebug() << "Error:" << code; QCoreApplication::quit(); }
};

#include "main.moc"

int main(int argc, char **argv)
{
     QCoreApplication a(argc, argv);

     QNetworkAccessManager networkAccessManager;
     networkAccessManager.setNetworkAccessible(QNetworkAccessManager::Accessible);

     QNetworkRequest networkRequest;
     networkRequest.setUrl(QUrl("http://test.com/test/rest/BOB"));

     Download download;

     // QObject::connect(&networkRequest, SIGNAL(finished()), &download, SLOT(handleFinished()));
     QObject::connect(&networkAccessManager, SIGNAL(finished(QNetworkReply*)), &download, SLOT(handleFinished(QNetworkReply*)));

     // QObject::connect(&networkRequest, SIGNAL(error(QNetworkReply::NetworkError)), &download, SLOT(handleError(QNetworkReply::NetworkError)));

     networkAccessManager.get(networkRequest);

     return a.exec();
}

main.pro

TEMPLATE = app
TARGET = main
QT = core network
SOURCES += main.cpp

Build and Run

qmake && make
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top