<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QT - C++ - HTTP Request and Reply]]></title><description><![CDATA[<p dir="auto">I'm new to QT and I would like some help if could give it to me.</p>
<p dir="auto">So I'm trying to create a class which will look something like this</p>
<pre><code>class WeatherAPI : public QObject {
Q_OBJECT
public:
    explicit WeatherAPI(QObject *parent = nullptr);

public slots:
    void readData(QNetworkReply *reply);

private:
    QNetworkAccessManager *manager;
};
</code></pre>
<p dir="auto">Where the constructor will be:</p>
<pre><code>WeatherAPI::WeatherAPI(QObject *parent) : QObject(parent) {
    manager = new QNetworkAccessManager(this);
    QObject::connect(manager, SIGNAL(finished(QNetworkReply * )), this, SLOT(readData(QNetworkReply * )));
    QUrl url("http://dummy.restapiexample.com/api/v1/employees");
    manager-&gt;get(QNetworkRequest(url));
}
</code></pre>
<p dir="auto">and the <code>readData</code> function will be:</p>
<pre><code>void WeatherAPI::readData(QNetworkReply *reply) {
    if (reply-&gt;error() == QNetworkReply::NoError) {
        QStringList propertyNames;
        QStringList propertyKeys;

        QString strReply = (QString) reply-&gt;readAll();

        qDebug() &lt;&lt; strReply;

        QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());

        QJsonObject jsonObject = jsonResponse.object();

        QString data = jsonObject["status"].toString();
        qDebug() &lt;&lt; data;
    } else {
        qDebug() &lt;&lt; "ERROR";
    }
    delete reply;
}
</code></pre>
<p dir="auto">And the call of the WeatherAPI will be done into mainwindow.cpp like this.</p>
<pre><code>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
    ui-&gt;setupUi(this);

    WeatherAPI api(this);
}
</code></pre>
<p dir="auto">Because I just care about that request to be done successfully for the moment at least.</p>
<p dir="auto">Now the tricky part comes, when I'm going to run this program it will never enter into <code>readData</code><br />
function, I don't know why, so in other words, I'm <strong>NOT</strong> going to receive any reply.</p>
<p dir="auto">But, if I will change the constructor to be like this:</p>
<pre><code>WeatherAPI::WeatherAPI(QObject *parent) : QObject(parent) {
    QEventLoop eventLoop;
    QNetworkAccessManager manager;
    QObject::connect(&amp;manager, SIGNAL(finished(QNetworkReply * )), &amp;eventLoop, SLOT(quit()));
    QNetworkRequest req;
    QNetworkReply *reply;
    QString url = "http://dummy.restapiexample.com/api/v1/employees";
    req.setUrl(url);
    reply = manager.get(req);
    eventLoop.exec();
    QJsonDocument jsonPageDoc = QJsonDocument::fromJson(reply-&gt;readAll());
    QJsonObject jsonPageObj = jsonPageDoc.object();
    qDebug() &lt;&lt; jsonPageObj["status"].toString();
}
</code></pre>
<p dir="auto">Then everything is going to work properly, except that I can't do this because there is not going to be anything asynchrony, or in other words, everything is going to run on the main thread.<br />
Could any of you guys tell me what's wrong with my first approach and why is not working? Why my code is never going to execute the <code>readData</code> function?</p>
]]></description><link>https://forum.qt.io/topic/116886/qt-c-http-request-and-reply</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 23:50:23 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/116886.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 10 Jul 2020 11:58:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QT - C++ - HTTP Request and Reply on Fri, 10 Jul 2020 12:01:23 GMT]]></title><description><![CDATA[<p dir="auto">WeatherAPI api(this);</p>
<p dir="auto">This object no more exist once the contractor completes.</p>
]]></description><link>https://forum.qt.io/post/606019</link><guid isPermaLink="true">https://forum.qt.io/post/606019</guid><dc:creator><![CDATA[dheerendra]]></dc:creator><pubDate>Fri, 10 Jul 2020 12:01:23 GMT</pubDate></item><item><title><![CDATA[Reply to QT - C++ - HTTP Request and Reply on Fri, 10 Jul 2020 12:01:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vildnex">@<bdi>Vildnex</bdi></a> said in <a href="/post/606017">QT - C++ - HTTP Request and Reply</a>:</p>
<blockquote>
<p dir="auto">WeatherAPI api(this);</p>
</blockquote>
<p dir="auto">This is a local variable and will disappear as soon as constructor terminates.<br />
Either make it class member or allocate on the heap.</p>
]]></description><link>https://forum.qt.io/post/606018</link><guid isPermaLink="true">https://forum.qt.io/post/606018</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 10 Jul 2020 12:01:01 GMT</pubDate></item></channel></rss>