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. Why the post method doesn't send the json to API Rest in QT5?

Why the post method doesn't send the json to API Rest in QT5?

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 4 Posters 7.2k Views
  • 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.
  • T Offline
    T Offline
    trip
    wrote on last edited by
    #1

    Hi, I'm trying to send a json via a post request in Qt5. This is my situation. I have a Parser class that takes a QByteArray, always with different values, from another class through the SIGNAL and SLOT mechanism. But I want to take only certain values, which when taken, send them to another method of the same Parser class where sending them to API Rest. Unfortunately the json is not sent to my app.
    This is my parser.h

    ...
    class Parser : public QObject
    {
        Q_OBJECT
    
    public:
        Parser();
    
    public slots:
        void receivePackage(QByteArray &sendBuffer);
        void replyFinished(QNetworkReply *reply);
        void slotError(QNetworkReply::NetworkError code);
    
    public:
        QByteArray bufferToParse;
        QByteArray bufferToPacket;
        QQueue<QByteArray> queue;
        void sendPacketToAPI(QByteArray &bufferToPacket);
    
    private:
        QNetworkAccessManager *m_manager;
        quint8 m_speed;
        quint8 m_acceleration;
    };
    ...
    

    This is my parser.cpp

    Parser::Parser()
        : m_manager { new QNetworkAccessManager }
    {
            connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    }
    
    void Parser::receivePackage(QByteArray &sendBuffer){
        queue.enqueue(sendBuffer);
        while (!queue.isEmpty()) {
            bufferToParse = queue.dequeue();
            ...
            for (int i = 6; i < 262; i++) {
                bufferToPacket.append(bufferToParse.at(i));
            }
        sendPacketToAPI(bufferToPacket);
        ...        
    }
    
    void replyFinished(QNetworkReply *reply)
    {
        reply->deleteLater();
        qDebug() << "reply delete!";
        qDebug() << "https post_request done!";
    }
    
    
    void Parser::sendPacketToAPI(QByteArray &bufferToPacket) {
        m_a = bufferToPacket.at(0);
        m_b = bufferToPacket.at(1);
        QNetworkRequest request(QUrl ("https://..."));
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        QString json = QString("{\"speed\":\"%1\",\"acceleration\":\"%2\"}").arg(m_a).arg(m_b);
        m_manager->post(request, json.toUtf8());
    }
    
    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @trip The API must have returned back the error ?

      157

      1 Reply Last reply
      1
      • T Offline
        T Offline
        trip
        wrote on last edited by
        #3

        @p3c0 the strange thing is that I do not back any mistakes ... even from server logs

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @trip What does reply->errorString() print in your replyFinished slot ?

          157

          1 Reply Last reply
          1
          • T Offline
            T Offline
            trip
            wrote on last edited by
            #5

            @p3c0 in connect I can not get in, I do not know why.
            I did

            ...
            QNetworkReply *reply = m_manager->post(request, json.toUtf8());
            qDebug() << "~error" << reply->errorString();
            ...
            

            and gives me "Unknown error".

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @trip Check the error in replyFinished slot. You have already connected to the finished signal.

              connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
              

              157

              1 Reply Last reply
              1
              • T Offline
                T Offline
                trip
                wrote on last edited by
                #7

                @p3c0 I know that it is already connected, and I understood what you say but the code does not seem to execute that piece of code. it is as if the SIGNAL and SLOT not communicated with

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @trip It can take time for the reply to be returned.

                  157

                  1 Reply Last reply
                  1
                  • T Offline
                    T Offline
                    trip
                    wrote on last edited by
                    #9

                    @p3c0 I left it running for a while, but the replyFinished method seems as if it were not called

                    raven-worxR 1 Reply Last reply
                    0
                    • T trip

                      @p3c0 I left it running for a while, but the replyFinished method seems as if it were not called

                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      @trip
                      since it looks like you are posting to a https url, do you have OpenSSL properly configured?

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      1
                      • T Offline
                        T Offline
                        trip
                        wrote on last edited by
                        #11

                        @raven-worx
                        No, I have not configured. Do you think is the problem?

                        raven-worxR 1 Reply Last reply
                        0
                        • T trip

                          @raven-worx
                          No, I have not configured. Do you think is the problem?

                          raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by
                          #12

                          @trip
                          yes ;)
                          But you should get some SSL errors printed in the debugger console (during initialization)

                          On what system are you developing on?

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          1
                          • T Offline
                            T Offline
                            trip
                            wrote on last edited by
                            #13

                            @raven-worx
                            Okay. What is the right flow to follow to configure OpenSSL correctly?
                            In fact I like errors

                            qt.network.ssl: QSslSocket: can not resolve SSLv2_client_method
                            qt.network.ssl: QSslSocket: can not resolve SSLv2_server_method
                            

                            I'm using Ubuntu 16 and Qt Creator 4

                            raven-worxR 1 Reply Last reply
                            0
                            • T trip

                              @raven-worx
                              Okay. What is the right flow to follow to configure OpenSSL correctly?
                              In fact I like errors

                              qt.network.ssl: QSslSocket: can not resolve SSLv2_client_method
                              qt.network.ssl: QSslSocket: can not resolve SSLv2_server_method
                              

                              I'm using Ubuntu 16 and Qt Creator 4

                              raven-worxR Offline
                              raven-worxR Offline
                              raven-worx
                              Moderators
                              wrote on last edited by raven-worx
                              #14

                              @trip
                              yea, about those errors i was talking.
                              Since you are on Ubuntu simply install the OpenSSL binaries (via the system package manager) and it should work.

                              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                              If you have a question please use the forum so others can benefit from the solution in the future

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                trip
                                wrote on last edited by
                                #15

                                @raven-worx
                                I had already installed the package. I run the program but I always make the same error.

                                1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  trip
                                  wrote on last edited by
                                  #16

                                  @raven-worx
                                  Should I use the connectToHostEncrypted method of the QNetworkAccessManager class?

                                  T raven-worxR 2 Replies Last reply
                                  0
                                  • T trip

                                    @raven-worx
                                    Should I use the connectToHostEncrypted method of the QNetworkAccessManager class?

                                    T Offline
                                    T Offline
                                    TheGringerEye
                                    wrote on last edited by
                                    #17

                                    @trip For using https enough call message get/put/post with https urls, for example

                                    QNetworkRequest req;
                                    req.setUrl(QUrl("https://cloud-api.yandex.net/v1/disk/"));
                                    

                                    and this is working in my program.

                                    I try to learn English.

                                    1 Reply Last reply
                                    0
                                    • T trip

                                      @raven-worx
                                      Should I use the connectToHostEncrypted method of the QNetworkAccessManager class?

                                      raven-worxR Offline
                                      raven-worxR Offline
                                      raven-worx
                                      Moderators
                                      wrote on last edited by
                                      #18

                                      @trip

                                      it has to be in a path where your application can find it.

                                      no not needed. Should all happen internally. Just make sure your application can find the OpenSSL binaries.

                                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                      If you have a question please use the forum so others can benefit from the solution in the future

                                      T 1 Reply Last reply
                                      0
                                      • raven-worxR raven-worx

                                        @trip

                                        it has to be in a path where your application can find it.

                                        no not needed. Should all happen internally. Just make sure your application can find the OpenSSL binaries.

                                        T Offline
                                        T Offline
                                        TheGringerEye
                                        wrote on last edited by
                                        #19

                                        @raven-worx for example, we can put together with the executable file.

                                        I try to learn English.

                                        1 Reply Last reply
                                        0
                                        • T Offline
                                          T Offline
                                          trip
                                          wrote on last edited by
                                          #20

                                          @raven-worx
                                          I tried to do another project where there is just main.cpp and where do

                                          #include <QCoreApplication>
                                          #include <QNetworkAccessManager>
                                          #include <QNetworkReply>
                                          #include <QNetworkRequest>
                                          #include <QDebug>
                                          #include <QObject>
                                          #include <QByteArray>
                                          #include <QUrl>
                                          
                                          
                                          void replyFinished(QNetworkReply *reply)
                                          {
                                              reply->deleteLater();
                                              qDebug() << "reply delete!";
                                              qDebug() << "https post_request done!";
                                          }
                                          
                                          int main(int argc, char *argv[])
                                          {
                                              QCoreApplication a(argc, argv);
                                          
                                              QNetworkAccessManager *manager = new QNetworkAccessManager();
                                          
                                              QNetworkRequest request(QUrl ("https://cryptic-reaches-94837.herokuapp.com/data/"));
                                          
                                              request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
                                          
                                              QObject::connect(manager, &QNetworkAccessManager::finished, replyFinished);
                                          
                                              quint8 speed = 0x12;
                                              quint8 acceleration = 0x5b;
                                          
                                              QString json = QString("{\"speed\":\"%1\",\"acceleration\":\"%2\"}").arg(speed).arg(acceleration);
                                          
                                              manager->post(request, json.toUtf8());
                                          
                                              return a.exec();
                                          }
                                          

                                          and it's work!!!
                                          but how come if add this piece of code to my project, why does not work to me?

                                          raven-worxR 1 Reply Last reply
                                          0

                                          • Login

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