Sending parameters by get method to rest api
-
@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.
wrote on 29 Jun 2022, 17:58 last edited by@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(); }
wrote on 29 Jun 2022, 18:07 last edited by@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.
wrote on 29 Jun 2022, 19:50 last edited by ali-aydin@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?
-
-
wrote on 30 Jun 2022, 08:05 last edited by
@Axel-Spoerl
Yes I tried by postman and is ok -
@Axel-Spoerl
Yes I tried by postman and is okwrote on 30 Jun 2022, 09:10 last edited by@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? -
wrote on 30 Jun 2022, 09:46 last edited by
@ali-aydin Look in your web server's documentation.
-
@ali-aydin Look in your web server's documentation.
-
wrote on 30 Jun 2022, 12:50 last edited by
I think you have to add 'Basic ' before your token when you set the Authorization header (or 'Bearer ' if you are doing bearer token auth). If things are working in Postman, check the 'Headers' tab of your request as to what is the actual value being sent for the Authorization.
-
I think you have to add 'Basic ' before your token when you set the Authorization header (or 'Bearer ' if you are doing bearer token auth). If things are working in Postman, check the 'Headers' tab of your request as to what is the actual value being sent for the Authorization.
wrote on 30 Jun 2022, 13:59 last edited by ali-aydin@mchinand
Bearer must be added to the token.
in postman there is no header
in authorization tab
only added token and in parameters tab is only one parameter :KalaCode
in body tab username,password and grant_type
in the qt when I want to authenticate byQUrl url("http://localhost:594444/api/authenticate");
it returns "Hello"
but when i changing url toQUrl url("http://localhost:594444/api/Getmojodi" );
it does not work
-
wrote on 30 Jun 2022, 14:05 last edited by mchinand
-
I meant the 'Headers' tab of the Postman request:
The authorization header value above was automatically set when I added a (junk) username and password in the Auth tab:
-
wrote on 30 Jun 2022, 14:24 last edited by
Are you setting the Authorization the same in Qt as the value it is set in Postman? Your variables are 'basic_authorization' and 'token', is token just the token or is the token type prepended to it ('Basic ' or 'Bearer ')? Your variable is 'basic_authorization' but in your prior message, you mention adding 'Bearer' before the token, which auth method are you using?
-
Are you setting the Authorization the same in Qt as the value it is set in Postman? Your variables are 'basic_authorization' and 'token', is token just the token or is the token type prepended to it ('Basic ' or 'Bearer ')? Your variable is 'basic_authorization' but in your prior message, you mention adding 'Bearer' before the token, which auth method are you using?
16/40