I'd like to write a small HTTP GET
request in C++ but I'm not finding a solution.
Maybe I have to use QNetworkAccessManager
class but I'm not an expert in C++ and it's really hard to understand something about this class.
I have an URL to fetch, which will give me a JSON response; I have to extract a single key (key2
) and put the value in a variable. This is an example:
{"key1": "value1", "key2": "**value2**", "key3": "value3"}
This is my debug code:
char value[20]
value = <**value2**>
After this I have to print this value in a form .ui
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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With