Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sending parameters by get method to rest api
Forum Updated to NodeBB v4.3 + New Features

Sending parameters by get method to rest api

Scheduled Pinned Locked Moved Solved General and Desktop
40 Posts 5 Posters 5.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 29 Jun 2022, 12:30 last edited by
    #2

    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.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    A 1 Reply Last reply 29 Jun 2022, 12:33
    1
    • S SGaist
      29 Jun 2022, 12:30

      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.

      A Offline
      A Offline
      ali-aydin
      wrote on 29 Jun 2022, 12:33 last edited by
      #3

      @SGaist
      your mean that i should use reply's finish signal?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 29 Jun 2022, 12:37 last edited by
        #4

        Yes, exactly.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply 29 Jun 2022, 12:46
        0
        • S SGaist
          29 Jun 2022, 12:37

          Yes, exactly.

          A Offline
          A Offline
          ali-aydin
          wrote on 29 Jun 2022, 12:46 last edited by
          #5

          @SGaist
          No that is not ok
          can you give an example?please

          1 Reply Last reply
          1
          • A ali-aydin
            29 Jun 2022, 11:58

            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.
            
            A Online
            A Online
            Axel Spoerl
            Moderators
            wrote on 29 Jun 2022, 16:06 last edited by Axel Spoerl
            #6

            @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 and exec() 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.
            

            Software Engineer
            The Qt Company, Oslo

            A 1 Reply Last reply 29 Jun 2022, 17:58
            1
            • A Axel Spoerl
              29 Jun 2022, 16:06

              @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 and exec() 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.
              
              A Offline
              A Offline
              ali-aydin
              wrote on 29 Jun 2022, 17:58 last edited by
              #7

              @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();
                  }
              
              J 1 Reply Last reply 29 Jun 2022, 18:07
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 29 Jun 2022, 18:03 last edited by
                #8

                You are not doing any error management so you can't know what is happening.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                A 1 Reply Last reply 29 Jun 2022, 18:22
                1
                • A ali-aydin
                  29 Jun 2022, 17:58

                  @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();
                      }
                  
                  J Offline
                  J Offline
                  JonB
                  wrote on 29 Jun 2022, 18:07 last edited by
                  #9

                  @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?

                  1 Reply Last reply
                  0
                  • S SGaist
                    29 Jun 2022, 18:03

                    You are not doing any error management so you can't know what is happening.

                    A Offline
                    A Offline
                    ali-aydin
                    wrote on 29 Jun 2022, 18:22 last edited by
                    #10

                    @SGaist
                    there is not error
                    because I can get token and make authorization

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 29 Jun 2022, 18:26 last edited by
                      #11

                      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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      A 1 Reply Last reply 29 Jun 2022, 18:34
                      0
                      • S SGaist
                        29 Jun 2022, 18:26

                        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.

                        A Offline
                        A Offline
                        ali-aydin
                        wrote on 29 Jun 2022, 18:34 last edited by
                        #12

                        @SGaist
                        so how does it work?
                        I can not find any way.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 29 Jun 2022, 18:36 last edited by
                          #13

                          The best place is the documentation: QNetworkReply::errorOccured.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          A 1 Reply Last reply 29 Jun 2022, 19:50
                          2
                          • S SGaist
                            29 Jun 2022, 18:36

                            The best place is the documentation: QNetworkReply::errorOccured.

                            A Offline
                            A Offline
                            ali-aydin
                            wrote on 29 Jun 2022, 19:50 last edited by ali-aydin
                            #14

                            @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?

                            1 Reply Last reply
                            0
                            • A Online
                              A Online
                              Axel Spoerl
                              Moderators
                              wrote on 30 Jun 2022, 05:43 last edited by
                              #15

                              403 is what your server sends. It means that your parameters are incorrect or the server itself has (due to misconfiguration) no access to the requested resource.
                              Have you tried the call with postman or a browser?

                              Software Engineer
                              The Qt Company, Oslo

                              A 1 Reply Last reply 30 Jun 2022, 08:05
                              0
                              • A Axel Spoerl
                                30 Jun 2022, 05:43

                                403 is what your server sends. It means that your parameters are incorrect or the server itself has (due to misconfiguration) no access to the requested resource.
                                Have you tried the call with postman or a browser?

                                A Offline
                                A Offline
                                ali-aydin
                                wrote on 30 Jun 2022, 08:05 last edited by
                                #16

                                @Axel-Spoerl
                                Yes I tried by postman and is ok

                                J 1 Reply Last reply 30 Jun 2022, 09:10
                                0
                                • A ali-aydin
                                  30 Jun 2022, 08:05

                                  @Axel-Spoerl
                                  Yes I tried by postman and is ok

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 30 Jun 2022, 09:10 last edited by
                                  #17

                                  @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?

                                  A 1 Reply Last reply 30 Jun 2022, 09:40
                                  0
                                  • J JonB
                                    30 Jun 2022, 09:10

                                    @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?

                                    A Offline
                                    A Offline
                                    ali-aydin
                                    wrote on 30 Jun 2022, 09:40 last edited by
                                    #18

                                    @JonB
                                    OS is Windows 10 and qt 5.15.2

                                    J 1 Reply Last reply 30 Jun 2022, 09:46
                                    0
                                    • A ali-aydin
                                      30 Jun 2022, 09:40

                                      @JonB
                                      OS is Windows 10 and qt 5.15.2

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 30 Jun 2022, 09:46 last edited by
                                      #19

                                      @ali-aydin Look in your web server's documentation.

                                      A 1 Reply Last reply 30 Jun 2022, 11:39
                                      0
                                      • J JonB
                                        30 Jun 2022, 09:46

                                        @ali-aydin Look in your web server's documentation.

                                        A Offline
                                        A Offline
                                        ali-aydin
                                        wrote on 30 Jun 2022, 11:39 last edited by
                                        #20

                                        @JonB
                                        API's documentation?

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          mchinand
                                          wrote on 30 Jun 2022, 12:50 last edited by
                                          #21

                                          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.

                                          A 1 Reply Last reply 30 Jun 2022, 13:59
                                          0

                                          11/40

                                          29 Jun 2022, 18:26

                                          topic:navigator.unread, 29
                                          • Login

                                          • Login or register to search.
                                          11 out of 40
                                          • First post
                                            11/40
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved