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. problem in sending a json file to url
Qt 6.11 is out! See what's new in the release blog

problem in sending a json file to url

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 2.3k 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.
  • KroMignonK KroMignon

    @JoeCFD said in problem in sending a json file to url:

    What is wrong? I also wrote out json_document and made sure its contents is same as test.json

    You have to wait until post request processing is done!
    ==> see QNetworkReply

    Something like this:

    QNetworkAccessManager manager;
    auto reply = manager.post( json_request, json_document.toJson() );
    
    connect(reply, &QNetworkReply::finished, this, [reply]()
        {
            qDebug() << "Done" << qPrintable( reply->errorString() );
        });
    
    JoeCFDJ Offline
    JoeCFDJ Offline
    JoeCFD
    wrote on last edited by
    #4

    @KroMignon Thanks for your reply. I will check that out.

    1 Reply Last reply
    0
    • KroMignonK KroMignon

      @JoeCFD said in problem in sending a json file to url:

      What is wrong? I also wrote out json_document and made sure its contents is same as test.json

      You have to wait until post request processing is done!
      ==> see QNetworkReply

      Something like this:

      QNetworkAccessManager manager;
      auto reply = manager.post( json_request, json_document.toJson() );
      
      connect(reply, &QNetworkReply::finished, this, [reply]()
          {
              qDebug() << "Done" << qPrintable( reply->errorString() );
          });
      
      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #5
      This post is deleted!
      1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #6
           added:
            if ( m_reply->isRunning() ) {
                connect( m_reply, &QNetworkReply::finished,
                         this,    &ClassName::postFinished );
            }
            else {
                postFinished();
            }
        

        void ClassName::postFinished()
        {
        std::cout << " error " << qPrintable( m_reply->errorString() ) << std::endl;
        }

        the output in slot postFinished()
        Unknown error

        JonBJ KroMignonK 2 Replies Last reply
        0
        • JoeCFDJ JoeCFD
             added:
              if ( m_reply->isRunning() ) {
                  connect( m_reply, &QNetworkReply::finished,
                           this,    &ClassName::postFinished );
              }
              else {
                  postFinished();
              }
          

          void ClassName::postFinished()
          {
          std::cout << " error " << qPrintable( m_reply->errorString() ) << std::endl;
          }

          the output in slot postFinished()
          Unknown error

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #7

          @JoeCFD
          Why are you expecting anything in errorString()? How do you know any error is occurring?

          If I wanted to know about possible errors I would slot onto https://doc.qt.io/qt-5/qnetworkreply.html#errorOccurred.

          JoeCFDJ 1 Reply Last reply
          1
          • JonBJ JonB

            @JoeCFD
            Why are you expecting anything in errorString()? How do you know any error is occurring?

            If I wanted to know about possible errors I would slot onto https://doc.qt.io/qt-5/qnetworkreply.html#errorOccurred.

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #8

            @JonB The json file is supposed to change something in the URL. The data was not changed before. It is working now. The errorString() should return empty string if there is no error.

            JonBJ 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              @JonB The json file is supposed to change something in the URL. The data was not changed before. It is working now. The errorString() should return empty string if there is no error.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #9

              @JoeCFD said in problem in sending a json file to url:

              The errorString() should return empty string if there is no error.

              Could you explain that? And what value was https://doc.qt.io/qt-5/qnetworkreply.html#error?

              JoeCFDJ 1 Reply Last reply
              0
              • JonBJ JonB

                @JoeCFD said in problem in sending a json file to url:

                The errorString() should return empty string if there is no error.

                Could you explain that? And what value was https://doc.qt.io/qt-5/qnetworkreply.html#error?

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #10

                @JonB this one is 0. It is right. The code did not work before. I was checking error message from errorString(). This func should return something like no error or empty. Unknown error is not clear.

                JonBJ 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  @JonB this one is 0. It is right. The code did not work before. I was checking error message from errorString(). This func should return something like no error or empty. Unknown error is not clear.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #11

                  @JoeCFD
                  :) You don't like a Qt error string, I am not responsible for what it prints and I don't see the docs stating it should return your wish! Earlier today, I was answering some post where the OP didn't like the "no error" text either, was that you? Maybe I'm going senile..., never mind :) I suggest you always check an error number if it is available rather than a string, if you are developing code. But I'm glad your code is working now.

                  JoeCFDJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @JoeCFD
                    :) You don't like a Qt error string, I am not responsible for what it prints and I don't see the docs stating it should return your wish! Earlier today, I was answering some post where the OP didn't like the "no error" text either, was that you? Maybe I'm going senile..., never mind :) I suggest you always check an error number if it is available rather than a string, if you are developing code. But I'm glad your code is working now.

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #12

                    @JonB First, I appreciate your reply. Unknown error is kind of confusing for me. I did not ask you to change the output of errorString(). And I did not post anywhere else except in this topic today.

                    1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD
                         added:
                          if ( m_reply->isRunning() ) {
                              connect( m_reply, &QNetworkReply::finished,
                                       this,    &ClassName::postFinished );
                          }
                          else {
                              postFinished();
                          }
                      

                      void ClassName::postFinished()
                      {
                      std::cout << " error " << qPrintable( m_reply->errorString() ) << std::endl;
                      }

                      the output in slot postFinished()
                      Unknown error

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by
                      #13

                      @JoeCFD said in problem in sending a json file to url:

                      void ClassName::postFinished()
                      {
                      std::cout << " error " << qPrintable( m_reply->errorString() ) << std::endl;
                      }
                      the output in slot postFinished()
                      Unknown error

                      This was only an example to explain how-to get in touch when request is done!
                      I just rewrite your code to made it work.
                      A more logical process would be:

                      connect(reply, &QNetworkReply::finished, this, [reply]()
                          {
                              if (reply->error() == QNetworkReply::NoError)
                              {
                                  QByteArray data = reply->readAll();
                                  QDebug() << "Success:" << data;
                              }
                              else
                                  qDebug() << "Failure: " << qPrintable( reply->errorString() );
                          });
                      

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      1 Reply Last reply
                      3

                      • Login

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