c++jsonqt

TLS initialization failed on GET Request


I'm starting with qt/c++ and on my project I need to make a json post/get/put but I'm trying to do a simple get request but I have an error: qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed

I don't know why, I tried some methods but all put the same message. I attach the code that I use.

mainwindow.cpp:

void MainWindow::replyFini(QNetworkReply* reply)
{
    QString answer = QString::fromUtf8(reply->readAll());
    qDebug() << "answer------------>"<<answer;
}

void MainWindow::on_btn_login_clicked()
{
    QNetworkRequest request(QUrl("https://httpbin.org/get"));
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFini(QNetworkReply*)));
    manager->get(request);
}

mainwindow.h:

#include <QtCore>
#include <QNetworkAccessManager>
#include <QNetworkReply>

Solution

  • The SSL libraries are not found. The error can be read out in the Qt Src. Either deploy them with your application or install OpenSSL on your machine.

    Also, just a small hint:

    void MainWindow::replyFini(QNetworkReply* reply)
    {
        QString answer = QString::fromUtf8(reply->readAll());
        qDebug() << "answer------------>"<<answer;
    }
    

    You should call reply->deleteLater();

    void MainWindow::on_btn_login_clicked()
    {
        QNetworkRequest request(QUrl("https://httpbin.org/get"));
        QNetworkAccessManager *manager = new QNetworkAccessManager(this);
        connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFini(QNetworkReply*)));
        manager->get(request);
    }
    

    Add connect(manager, &QNetworkAccessManager::finished, manager, &QNetworkAccessManager::deleteLater);to avoid a memory leak on each click.