Send GET Request using C++

Qt Network module

  1. Add linking against the module library:

If building project with CMake then use the find_package() command to locate the needed module:

find_package(Qt5 COMPONENTS Network REQUIRED)
target_link_libraries(myapp Qt5::Network)
  1. Send GET request:
#include <iostream>
#include <QCoreApplication>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QEventLoop>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QNetworkRequest request(QUrl("https://httpbin.org/get"));

    QNetworkAccessManager *manager = new QNetworkAccessManager();
    QNetworkReply *reply = manager->get(request);

    QEventLoop eventLoop;
    QObject::connect(manager, &QNetworkAccessManager::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();

    QString content = reply->readAll();

    std::cout << content.toStdString() << std::endl;

    return app.exec();
}

Leave a Comment

Cancel reply

Your email address will not be published.