Read Json from https in qt - c++

I wrote this code on qt, but when i run this project, My output is "Error".
How can solve my problem?
For example in this code I add a address in url, and I want read Json from this url, and show some info.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QNetworkAccessManager* nam = new QNetworkAccessManager(this);
QString test = "ar";
QString test2 = "Hello World";
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(onResult(QNetworkReply*)));
QUrl url("https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20180627T161429Z.7e64c91dd2016a6c.9901da9a44bc324388a2460776ab55b2d72b4c5a&lang=" + test + "&text=" + test2);
QNetworkReply* reply = nam->get(QNetworkRequest(url));
}
void MainWindow::onResult(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError) {
QStringList propertyNames;
QStringList propertyKeys;
QString strReply = (QString)reply->readAll();
qDebug() << strReply;
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObject = jsonResponse.object();
QJsonArray jsonArray = jsonObject["status"].toArray();
qDebug() << jsonObject["status"].toString();
foreach (const QJsonValue & value, jsonArray)
{
QJsonObject obj = value.toObject();
qDebug() << value.toString();
}
} else {
qDebug() << "ERROR";
}
delete reply;
}

To add key-values ​​to the url you must use QUrlQuery as shown below:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
nam = new QNetworkAccessManager(this);
connect(nam, &QNetworkAccessManager::finished, this, &MainWindow::onResult);
QString lang = "ar";
QString text = "Hello World";
QString key = "trnsl.1.1.20180627T161429Z.7e64c91dd2016a6c.9901da9a44bc324388a2460776ab55b2d72b4c5a";
QUrlQuery query;
query.addQueryItem("key", key);
query.addQueryItem("lang", lang);
query.addQueryItem("text", text);
QUrl url("https://translate.yandex.net/api/v1.5/tr.json/translate");
url.setQuery(query);
qDebug()<< "url: "<< url.toString(QUrl::FullyEncoded);
nam->get(QNetworkRequest(url));
}
void MainWindow::onResult(QNetworkReply *reply){
if(reply->error() == QNetworkReply::NoError){
QByteArray result = reply->readAll();
QJsonDocument jsonResponse = QJsonDocument::fromJson(result);
QJsonObject obj = jsonResponse.object();
qDebug()<<"code: " << obj["code"].toInt();
qDebug()<<"lang: " << obj["lang"].toString();
QJsonArray array = obj["text"].toArray();
for(const QJsonValue & value : array) {
qDebug()<< "text: " <<value.toString();
}
}
else
qDebug() << "ERROR";
reply->deleteLater();
}
Output:
url: "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20180627T161429Z.7e64c91dd2016a6c.9901da9a44bc324388a2460776ab55b2d72b4c5a&lang=ar&text=Hello%20World"
code: 200
lang: "en-ar"
text: "مرحبا العالم"
If the url generated is revised, it differs from the concatenation:
Concatenation:
...&text=Hello World
Encoded:
...&text=Hello%20World

Related

QtCUrl post doesn´t work anymore (Linux nok...windows ok)

I have been running this function since last year (Linux and Windows) within my program.
Now I need to implement a new function and my new build is no longer running.
I have other CUrl functions using POST and the results are the same: nok, but my GET functions are ok.
I have another computer (with Mint 19) where this program is running smoothly, but on my computer (using Mint 19, too) the compilation is fine, but it starts curl.exec (I'm using Qtcurl library and inside has a call to curl_easy_perform) and no longer returns.
I have this package installed: libcurl4-openssl-dev
It's okay to compile my program (Linux and Windows). This program is running on Windows.
My problem is just new builds in Mint19.
What is missing to install?
QUrl url("https://pos-api.ifood.com.br/oauth/token");
QUrlQuery q;
q.addQueryItem("client_id", id);
q.addQueryItem("client_secret", secret);
q.addQueryItem("grant_type","password");
q.addQueryItem("username",user);
q.addQueryItem("password",password);
url.setQuery(q);
QtCUrl::Options opt;
opt[CURLOPT_URL] = url;
opt[CURLOPT_POST] = true;
opt[CURLOPT_FOLLOWLOCATION] = true;
opt[CURLOPT_FAILONERROR] = true;
opt[CURLOPT_SSL_VERIFYPEER]= false; // windows
QStringList headers;
headers
<< "cache-control: no-cache"
<< "content-type: application/x-www-form-urlencoded";
opt[CURLOPT_HTTPHEADER] = headers;
val = cUrl.exec(opt); // PROBLEM HERE!!!!
if (cUrl.lastError().isOk()) {
bool ok;
// json is a QString containing the JSON data
QtJson::JsonObject result = QtJson::parse(val, ok).toMap();
token=result["access_token"].toString();
return token;
}
else {
return "";
}
I changed all my methods.
The first funcion is a POST with query.
QString iFood_getToken2(QString token, int *expira, QString id, QString secret, QString user, QString password, QString host){
if(host!=hostname || !ifood_ativo){
qDebug() << "iFood_getToken2 saindo...";
return "";
}
if(*expira>IFOOD_TASK){
*expira-=IFOOD_TASK;
// qDebug() << "expira " << *expira;
return token; // token válido
}
QUrl url("https://pos-api.ifood.com.br/oauth/token");
QUrlQuery q;
q.addQueryItem("client_id", id);
q.addQueryItem("client_secret", secret);
q.addQueryItem("grant_type","password");
q.addQueryItem("username",user);
q.addQueryItem("password",password);
url.setQuery(q);
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QVariant(int(QNetworkRequest::AlwaysNetwork)));
QJsonObject json;
QNetworkAccessManager nam;
QNetworkReply *reply = nam.post(request, QJsonDocument(json).toJson());
while (!reply->isFinished())
{
qApp->processEvents();
}
QByteArray response_data = reply->readAll();
QJsonDocument jsonr = QJsonDocument::fromJson(response_data);
reply->deleteLater();
//qDebug() << "ifoodtoken2 " << jsonr["access_token"].toString();
return jsonr["access_token"].toString();
}
I did implement these new functions:
There is a new implementation for a GET and PATCH
So, from now on I dont need to use CUrl library anymore
QJsonDocument networkGet(QString strUrl, QString token){
QUrl url(strUrl);
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QVariant(int(QNetworkRequest::AlwaysNetwork)));
QString headerData = "bearer " + token;
request.setRawHeader("Authorization", headerData.toLocal8Bit());
QJsonObject json;
QNetworkAccessManager nam;
QNetworkReply *reply = nam.get(request);
while (!reply->isFinished())
{
qApp->processEvents();
}
QByteArray response_data = reply->readAll();
QJsonDocument json_response = QJsonDocument::fromJson(response_data);
reply->deleteLater();
//qDebug() << "networkGet " << json_response << reply->errorString() << headerData ;
return json_response;
}
int networkPatch(QString strUrl, QString token, QJsonDocument json){
QUrl url(strUrl);
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QVariant(int(QNetworkRequest::AlwaysNetwork)));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QString headerData = "bearer " + token;
request.setRawHeader("Authorization", headerData.toLocal8Bit());
QNetworkAccessManager nam;
QByteArray * _b_arr = new QByteArray (QString(json.toJson()).toLatin1());
QBuffer *_qbf_upload =new QBuffer (_b_arr);
QNetworkReply *reply = nam.sendCustomRequest(request,"PATCH",_qbf_upload);
while (!reply->isFinished())
{
qApp->processEvents();
}
QByteArray response_data = reply->readAll();
QJsonDocument json_response = QJsonDocument::fromJson(response_data);
reply->deleteLater();
qDebug() << "networkPatch " << reply->error() << json_response << reply->errorString() << headerData ;
return reply->error();
}

QJsonDocument to list or array in c++

I've tried this code and works, but I didn't understand how can get json and convert in array or list with Qt.
My code:
QEventLoop eventLoop;
QNetworkAccessManager mgr;
QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QNetworkRequest req(QUrl(QString("http://myurljson.com/getjson")));
QNetworkReply *reply = mgr.get(req);
eventLoop.exec(); // blocks stack until "finished()" has been called
if (reply->error() == QNetworkReply::NoError) {
QString strReply = (QString)reply->readAll();
qDebug() << "Response:" << strReply;
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObj = jsonResponse.object();
qDebug() << "test:" << jsonObj["MCC_Dealer"].toString();
qDebug() << "test1:" << jsonObj["MCC_User"].toString();
delete reply;
}
else {
//failure
qDebug() << "Failure" <<reply->errorString();
delete reply;
}
my json get (3 records from url):
[{"MCC_Dealer":'test',"MCC_User":'test',"CurrentDealer":'test',"CurrentUser":'test'},{"MCC_Dealer":'test',"MCC_User":'test',"CurrentDealer":'test',"CurrentUser":'test'},{"MCC_Dealer":'test',"MCC_User":'test',"CurrentDealer":'test',"CurrentUser":'test'}]
I need to get json and set in list or in array.
My target is convert json response in array or list with c++ and Qt.
Any ideas?
Thanks
As I have mentioned in my comments, your JSON response is already an array, so you don't need to create additional structures to store the data you got. In order to de-serialize your data you can do the following:
[..]
QJsonArray jsonArray = jsonResponse.array();
for (auto it = jsonArray.constBegin(); it != jsonArray.constEnd(); ++it)
{
const QJsonValue &val = *it;
// We expect that array contains objects like:
// {"MCC_Dealer":'test',"MCC_User":'test',"CurrentDealer":'test',"CurrentUser":'test'}
QJsonObject o = val.toObject();
// Iterate over all sub-objects. They all have string values.
for (auto oIt = o.constBegin(); oIt != o.constEnd(); ++oIt)
{
// "MCC_Dealer":'test'
qDebug() << "Key:" << oIt.key() << ", Value:" << oIt.value().toString();
}
}

Refresh QTableWidget automatically possible?

QTableWidget(in my code, ipTable) Item come from test_data.txt.
But test_data.txt file change in every 3seconds.
I want refresh the table automatically..
How can I update QTableWidget automatically..?
This is my code.
#include "dialog.h"
#include "ui_dialog.h"
#include "addip.h"
#include <QFile>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(onTimer()));
timer->start(1000);
setWindowTitle( "IP List" );
ui->ipTable->setColumnCount(3);
refresh_table();
}
Dialog::~Dialog()
{
delete ui;
}
QStringList Dialog::refresh_table()
{
int field;
QFile file( "/home/yein/widget/test_data.txt" );
QStringList title;
title << "IP" << "Protocol" << "state";
file.open( QIODevice::ReadOnly);
QTextStream read(&file);
ui->ipTable->clear();
ui->ipTable->setRowCount(0);
ui->ipTable->setHorizontalHeaderLabels(title);
while(!read.atEnd())
{
QString tmp = read.readLine();
QStringList tmpList = tmp.split( "," );
ui->ipTable->insertRow(ui->ipTable->rowCount());
field = ui->ipTable->rowCount() - 1;
ui->ipTable->setItem( field, IP, new QTableWidgetItem( tmpList[0] ) );
ui->ipTable->setItem( field, PROTOCOL, new QTableWidgetItem( tmpList[1] ) );
ui->ipTable->setItem( field, STATE, new QTableWidgetItem( tmpList[2] ) );
}
file.close();
return table;
}
void Dialog::on_btnAdd_clicked()
{
QString protocol;
QString IP;
int res;
addIP add(this);
add.setWindowTitle( "Add IP" );
res = add.exec();
if( res == QDialog::Rejected )
return;
IP = add.getIP();
protocol = add.getProtocol();
qDebug() << "IP :" << " " << IP;
qDebug() << "Protocol : " << " " << protocol;
write_on_file( IP,protocol );
}
void Dialog::write_on_file( QString IP, QString protocol )
{
QFile file( "/home/yein/widget/test_data.txt" );
file.open( QIODevice::Append );
data[0] = IP;
data[1] = protocol;
data[2] = "0"; // init state 0
QString _str = QString( "%1,%2,%3\n" )
.arg( data[0] )
.arg( data[1] )
.arg( data[2] );
qDebug() << _str << " ";
QByteArray str;
str.append(_str);
file.write(str);
file.close();
refresh_table();
}
void Dialog::on_btnClose_clicked()
{
this->close();
}
void Dialog::onTimer()
{
updateRStatusBar();
}
void Dialog::updateRStatusBar()
{
QDateTime local(QDateTime::currentDateTime());
ui->clock->setText(local.toString());
}
One option is to use
QFileSystemWatcher::fileChanged(const QString &path)
and receive a signal, whenever the file is modified. This recommandation depends on how often the file is changed and on how many files you want to watch.

QNetworkReply has no data

I have a QWebView where I'm watching the network requests by connecting:
QObject::connect(page()->networkAccessManager(),
SIGNAL(finished(QNetworkReply*)),
this,
SLOT(networkLoaded(QNetworkReply*)));
then:
void browserControl::networkLoaded(QNetworkReply *reply)
{
const QUrl reqUrl = reply->request().url();
qDebug() << "url = " << reqUrl;
QByteArray array = reply->readAll();
QString data = QString::fromUtf8(array.data(), array.size());
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).value<int>();
qDebug() << "data = " << data;
qDebug() << "http code = " << statusCode;
}
But data is always empty, not matter if statusCode is 200. browserControl class is inheried from QWebView class.
You get no data because QWebPage read all data before your slot is called

QHttpMultiPart send post request results in "1"

I am currently using QHttpMultiPart in a Qt Project, but it seems to have some problems on my end ?
I have followed the example and came up with the following code:
#include "uploader.h"
#include <QFileInfo>
#include <QMimeDatabase>
#include <QHttpMultiPart>
#include <QNetworkReply>
#include <QDebug>
/**
* #brief Uploader::Uploader
* #param parent
*/
Uploader::Uploader(QObject *parent) :
QObject(parent)
{
uploadInProgress = false;
}
/**
* #brief Uploader::upload
* #param absoluteFilePath
*/
void Uploader::upload(QString absoluteFilePath)
{
qDebug() << "Upload Starting";
QFileInfo fileInfo(absoluteFilePath);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
//action part
QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"cmd\""));
textPart.setBody(QString("wFile").toLatin1());
//File Path
QHttpPart filePathPart;
filePathPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file_path\""));
filePathPart.setBody(absoluteFilePath.toLatin1());
//filepart
QHttpPart filePart;
QMimeDatabase db;
QMimeType mime = db.mimeTypeForFile(absoluteFilePath);
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(mime.name()));
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_file\"; filename=\""+ fileInfo.baseName() + "\""));
QFile *file = new QFile(absoluteFilePath);
if ( !file->exists() )
{
qDebug() << "File Does not exist";
}
file->open(QIODevice::ReadOnly);
filePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(textPart);
multiPart->append(filePathPart);
multiPart->append(filePart);
QUrl url("http://project.dbz.dev/index.php?controller=wapi&action=handle");
QNetworkRequest request(url);
pManager = new QNetworkAccessManager();
pReply = pManager->post(request, multiPart);
multiPart->setParent(pReply);
connect(pReply, SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(uploadProgress(qint64,qint64)));
connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));
connect(pReply, SIGNAL(finished()),this, SLOT(uploadFinished()));
// here connect signals etc.
uploadInProgress = true;
}
/**
* #brief Uploader::uploadFinished
*/
void Uploader::uploadFinished()
{
QString data = (QString) pReply->readAll();
qDebug() << data;
qDebug() << "Upload finished";
uploadInProgress = false;
if ( pReply->error() > 0 )
{
qDebug() << "Error occured: " << pReply->error() << " : " << pReply->errorString();
}
else
{
qDebug() << "Upload success";
}
delete pReply;
}
void Uploader::uploadProgress(qint64 a, qint64 b)
{
qDebug() << " SOME PROGRESS!";
qDebug() << a << "/" << b;
}
void Uploader::onError(QNetworkReply::NetworkError err)
{
qDebug() << " SOME ERROR!";
qDebug() << err;
}
Sadly, none of the SLOTS are triggered from the SIGNALS. Neither can I see a package send with wireshark on my local ethernet adapter.
However, my Apache does get a request:
192.168.178.21 - - [21/Sep/2013:05:10:41 +0200] "POST /index.php?controller=wapi&action=handle HTTP/1.1" 200 166 "-" "Mozilla/5.0"
And in my PHP Application I have the following outcome:
Application_Controller_WapiController::handleAction: Command: wFile
Application_Controller_WapiController::wFile: POST Request: 1
This, basically means, it recognises the Parameter "cmd" and the value "wFile", opens the according PHP action which then does a print_r($_POST) which shows me nothing more than a simple 1.
I have no idea what to do. I have looked everywhere on the internet and cannot seem to figure it out. I followed all examples and descriptions on the official documentary and found a couple of threads here on SO. There seemed to be a bug with the QHttpMultiPart class, although it was fixed with the major 5.0.0 update.
tl;dr:
connect(pReply, SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(uploadProgress(qint64,qint64)));
connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));
connect(pReply, SIGNAL(finished()),this, SLOT(uploadFinished()));
These signals are not being triggered, PHP print_r shows me a 1 and I cannot track the POST request on my machine.
It would be nice if somebody could tell me why the SIGNALS are not emitted and more importantly how I can see a final version of my POST request in my C++ application before it is sent.
Thank you very much! I appreciate any help!
I have solved the problem by adding:
pELoop = new QEventLoop();
pELoop->exec();
Which results into this:
#include "uploader.h"
#include <QFileInfo>
#include <QMimeDatabase>
#include <QHttpMultiPart>
#include <QNetworkReply>
#include <QDebug>
/**
* #brief Uploader::Uploader
* #param parent
*/
Uploader::Uploader(QObject *parent) :
QObject(parent)
{
uploadInProgress = false;
}
/**
* #brief Uploader::upload
* #param absoluteFilePath
*/
void Uploader::upload(QString absoluteFilePath)
{
qDebug() << "Upload Starting";
QFileInfo fileInfo(absoluteFilePath);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
//action part
QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"cmd\""));
textPart.setBody(QString("wFile").toLatin1());
//File Path
QHttpPart filePathPart;
filePathPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file_path\""));
filePathPart.setBody(absoluteFilePath.toLatin1());
//filepart
QHttpPart filePart;
QMimeDatabase db;
QMimeType mime = db.mimeTypeForFile(absoluteFilePath);
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(mime.name()));
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_file\"; filename=\""+ fileInfo.baseName() + "\""));
QFile *file = new QFile(absoluteFilePath);
if ( !file->exists() )
{
qDebug() << "File Does not exist";
}
file->open(QIODevice::ReadOnly);
filePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(textPart);
multiPart->append(filePathPart);
multiPart->append(filePart);
QUrl url("http://encryptor.dbz.dev/index.php?controller=wapi&action=handle");
QNetworkRequest request(url);
pManager = new QNetworkAccessManager();
pReply = pManager->post(request, multiPart);
multiPart->setParent(pReply);
pELoop = new QEventLoop();
connect(pReply, SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(uploadProgress(qint64,qint64)));
connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));
connect(pReply, SIGNAL(finished()),this, SLOT(uploadFinished()));
pELoop->exec();
// here connect signals etc.
uploadInProgress = true;
}
/**
* #brief Uploader::uploadFinished
*/
void Uploader::uploadFinished()
{
QString data = (QString) pReply->readAll();
qDebug() << data;
qDebug() << "Upload finished";
uploadInProgress = false;
if ( pReply->error() > 0 )
{
qDebug() << "Error occured: " << pReply->error() << " : " << pReply->errorString();
}
else
{
qDebug() << "Upload success";
}
pReply->deleteLater();
pELoop->exit();
}
void Uploader::uploadProgress(qint64 a, qint64 b)
{
qDebug() << " SOME PROGRESS!";
qDebug() << a << "/" << b;
}
void Uploader::onError(QNetworkReply::NetworkError err)
{
qDebug() << " SOME ERROR!";
qDebug() << err;
}
The request is executed as expected, and the signals are working as well.
I get the output of:
Upload Starting
SOME PROGRESS!
16384 / 483753
SOME PROGRESS!
483753 / 483753
SOME PROGRESS!
0 / 0
"Array
(
[controller] => wapi
[action] => handle
[cmd] => wFile
[file_path] => D:/Downloads/putty.exe
)
{"cmd":"","status":"","message":"","params":[]}"
Upload finished
Upload success
I leave this hear in case somebody is looking for a working example.