How do I write a Qt HTTP GET request?

I just managed to get HTTP requests working.

This example simply downloads google HTML page and prints its content to the terminal.

main.cpp

#include <iostream>
#include "main.h"

MyObject::MyObject(QApplication* app) {
    manager = new QNetworkAccessManager(app);
}

void MyObject::TestConnection() const {
auto status = connect(manager, &QNetworkAccessManager::finished,
                      this, &MyObject::ReplyFinished);
    qDebug() << "Connection status:" << status;

    manager->get(QNetworkRequest(QUrl("https://www.google.com")));
}

void MyObject::ReplyFinished(QNetworkReply *reply) {
    QString answer = reply->readAll();
    qDebug() << answer;
    QApplication::quit();
}

int main(int argc, char *argv[]) {
    auto *app = new QApplication(argc, argv);
    auto myObject = new MyObject(app);
    myObject->TestConnection();
    return QApplication::exec();
}

main.h

#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QApplication>

class MyObject : public QObject {
Q_OBJECT
public:
    explicit MyObject(QApplication* application);
    void TestConnection() const;
    static void ReplyFinished(QNetworkReply *reply);

    QNetworkAccessManager *manager;
};

To compile this on Linux with CMake, use following CMakeLists.txt:

cmake_minimum_required(VERSION 3.17)
project(http)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTOMOC ON) # important

add_executable(${PROJECT_NAME} main.cpp main.h)

find_package(Qt5Widgets REQUIRED)
find_package(Qt5Network REQUIRED) # important

target_link_libraries(${PROJECT_NAME} Qt5::Widgets Qt5::Network) # important

You need three things:

  • QNetworkAccessManager * manager; -> To send us a request.
  • QNetworkRequest request; -> what type of demand? get, post, ...
  • QNetworkReply. -> What's the answer?

for more detail:

  • http://doc.qt.io/qt-5/qnetworkaccessmanager.html#details

  • http://doc.qt.io/qt-5/qnetworkrequest.html#details

  • http://doc.qt.io/qt-5/qnetworkreply.html#details

for example:

.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    manager = new QNetworkAccessManager();
    QObject::connect(manager, &QNetworkAccessManager::finished,
        this, [=](QNetworkReply *reply) {
            if (reply->error()) {
                qDebug() << reply->errorString();
                return;
            }

            QString answer = reply->readAll();

            qDebug() << answer;
        }
    );
}

void MainWindow::on_pushButton_clicked()
{
    request.setUrl(QUrl("http://url"));
    manager->get(request);
}

MainWindow::~MainWindow()
{
    delete ui;
    delete manager;
}

.h file

private:
    Ui::MainWindow *ui;
    QNetworkAccessManager *manager;
    QNetworkRequest request;

EDIT LAMBDA SLOT: if not use lambda SIGNAL SLOT.

Discribe one slot in your .h file for example:

private slots:
    void managerFinished(QNetworkReply *reply);

in .cpp constructor replace lambda to

QObject::connect(manager, SIGNAL(finished(QNetworkReply*)),
    this, SLOT(managerFinished(QNetworkReply*)));

now in your slot:

void MainWindow::managerFinished(QNetworkReply *reply) {
    if (reply->error()) {
        qDebug() << reply->errorString();
        return;
    }

    QString answer = reply->readAll();

    qDebug() << answer;
}

Tags:

C++

Qt