Sending parameters by get method to rest api
-
I need to send parameters to Rest API by the get methods
i can get token and make authenticate but in sending other parameters there is some problems.my code is like this:QUrl url("http://localhost:59444/api/Getmojodi"); QNetworkRequest req1; const QByteArray basic_authorization =token.toUtf8().toBase64(); req1.setRawHeader(QByteArrayLiteral("Authorization"), basic_authorization); QUrlQuery uq; uq.addQueryItem("KalaCode","20101010131310"); url.setQuery(uq); req1.setUrl(url); QNetworkReply *rep= manager->get(req1); QString val = rep->readAll(); QJsonDocument jDoc=QJsonDocument::fromJson(val.toUtf8()); QJsonObject obj = jDoc.object(); qDebug()<<obj["mojodi"].toString();//is empty.
-
Hi,
Your problem is that you are reading the reply before the processing is finished. QNetworkAccessManager like many of Qt classes is asynchronous hence you have to use it that way.
-
Hi,
Your problem is that you are reading the reply before the processing is finished. QNetworkAccessManager like many of Qt classes is asynchronous hence you have to use it that way.
-
Yes, exactly.
-
I need to send parameters to Rest API by the get methods
i can get token and make authenticate but in sending other parameters there is some problems.my code is like this:QUrl url("http://localhost:59444/api/Getmojodi"); QNetworkRequest req1; const QByteArray basic_authorization =token.toUtf8().toBase64(); req1.setRawHeader(QByteArrayLiteral("Authorization"), basic_authorization); QUrlQuery uq; uq.addQueryItem("KalaCode","20101010131310"); url.setQuery(uq); req1.setUrl(url); QNetworkReply *rep= manager->get(req1); QString val = rep->readAll(); QJsonDocument jDoc=QJsonDocument::fromJson(val.toUtf8()); QJsonObject obj = jDoc.object(); qDebug()<<obj["mojodi"].toString();//is empty.
@ali-aydin said in Sending parameters by get method to rest api:
If you want your code to block until the reply has arrived, create an event loop on the stack, connect the network reply's finished signal to the quit slot of the event loop andexec()
it.QUrl url("http://localhost:59444/api/Getmojodi"); QNetworkRequest req1; const QByteArray basic_authorization =token.toUtf8().toBase64(); req1.setRawHeader(QByteArrayLiteral("Authorization"), basic_authorization); QUrlQuery uq; uq.addQueryItem("KalaCode","20101010131310"); url.setQuery(uq); req1.setUrl(url); QNetworkReply *rep= manager->get(req1); // blocking code starts here QEventLoop loop; QObject::connect(rep, &QNetworkReply::finished, &loop, &QEventLoop::quit); loop.exec(); // end of blocking code QString val = rep->readAll(); QJsonDocument jDoc=QJsonDocument::fromJson(val.toUtf8()); QJsonObject obj = jDoc.object(); qDebug()<<obj["mojodi"].toString();//is empty.
-
@ali-aydin said in Sending parameters by get method to rest api:
If you want your code to block until the reply has arrived, create an event loop on the stack, connect the network reply's finished signal to the quit slot of the event loop andexec()
it.QUrl url("http://localhost:59444/api/Getmojodi"); QNetworkRequest req1; const QByteArray basic_authorization =token.toUtf8().toBase64(); req1.setRawHeader(QByteArrayLiteral("Authorization"), basic_authorization); QUrlQuery uq; uq.addQueryItem("KalaCode","20101010131310"); url.setQuery(uq); req1.setUrl(url); QNetworkReply *rep= manager->get(req1); // blocking code starts here QEventLoop loop; QObject::connect(rep, &QNetworkReply::finished, &loop, &QEventLoop::quit); loop.exec(); // end of blocking code QString val = rep->readAll(); QJsonDocument jDoc=QJsonDocument::fromJson(val.toUtf8()); QJsonObject obj = jDoc.object(); qDebug()<<obj["mojodi"].toString();//is empty.
@Axel-Spoerl
Thanks for your help
but it is not ok not only this case but also I changed the code a bit
but is not ok yet
changed code:QUrl url("http://localhost:59444/api/Getmojodi"); QNetworkRequest req1; const QByteArray basic_authorization =token.toUtf8().toBase64(); req1.setRawHeader(QByteArrayLiteral("Authorization"), basic_authorization); QUrlQuery uq; uq.addQueryItem("KalaCode","20101010131310"); url.setQuery(uq); req1.setUrl(url); QNetworkReply *rep= manager->get(req1); // blocking code starts here QEventLoop loop; QTimer timer; timer.setSingleShot(true); QObject::connect(&timer,&QTimer::timeout,&loop,&QEventLoop::quit); QObject::connect(rep, &QNetworkReply::finished, &loop, &QEventLoop::quit); timer.start(5000); loop.exec(); // end of blocking code if(!timer.isActive()) { QString val = rep->readAll(); QJsonDocument jDoc=QJsonDocument::fromJson(val.toUtf8()); QJsonObject obj = jDoc.object(); qDebug()<<val;//obj["mojodi"].toString(); }
-
You are not doing any error management so you can't know what is happening.
-
@Axel-Spoerl
Thanks for your help
but it is not ok not only this case but also I changed the code a bit
but is not ok yet
changed code:QUrl url("http://localhost:59444/api/Getmojodi"); QNetworkRequest req1; const QByteArray basic_authorization =token.toUtf8().toBase64(); req1.setRawHeader(QByteArrayLiteral("Authorization"), basic_authorization); QUrlQuery uq; uq.addQueryItem("KalaCode","20101010131310"); url.setQuery(uq); req1.setUrl(url); QNetworkReply *rep= manager->get(req1); // blocking code starts here QEventLoop loop; QTimer timer; timer.setSingleShot(true); QObject::connect(&timer,&QTimer::timeout,&loop,&QEventLoop::quit); QObject::connect(rep, &QNetworkReply::finished, &loop, &QEventLoop::quit); timer.start(5000); loop.exec(); // end of blocking code if(!timer.isActive()) { QString val = rep->readAll(); QJsonDocument jDoc=QJsonDocument::fromJson(val.toUtf8()); QJsonObject obj = jDoc.object(); qDebug()<<val;//obj["mojodi"].toString(); }
@ali-aydin said in Sending parameters by get method to rest api:
but is not ok yet
if(!timer.isActive())
qDebug()<<val;
... and additionally to @SGaist why not tell us whether it times out or what it outputs?
-
Authentication, authorization and request management are three different stages that all can fail separately. The fact that you can authenticate does not mean that everything else is working.
-
Authentication, authorization and request management are three different stages that all can fail separately. The fact that you can authenticate does not mean that everything else is working.
-
The best place is the documentation: QNetworkReply::errorOccured.
-
The best place is the documentation: QNetworkReply::errorOccured.
@SGaist
@Axel-Spoerl
I changed slot that connected to finish signal like this :QByteArray bytes = R->readAll(); QString str = QString::fromUtf8(bytes.data(), bytes.size()); int statusCode = R->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug() << QVariant(statusCode).toString();
and return 403 code it means forbidden.why?
-
-
@Axel-Spoerl
Yes I tried by postman and is ok -
@Axel-Spoerl
Yes I tried by postman and is ok@ali-aydin
Your platform/web server may/should have the ability to log all requests/responses to a log file? You might switch that on and see if anything helpful there? -
@ali-aydin
Your platform/web server may/should have the ability to log all requests/responses to a log file? You might switch that on and see if anything helpful there? -
@ali-aydin Look in your web server's documentation.
-
@ali-aydin Look in your web server's documentation.