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
Forum Updated to NodeBB v4.3 + New Features

problem in sending a json file to url

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 961 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.
  • JoeCFDJ JoeCFD

    from command line
    curl -u username:password -d @test.json http://.../
    works fine. The return is
    {
    "OK" : true,
    "Message" : "Configured Successfully"
    }

    test.json:
    {
    "General": {
    "Name": "cat"
    }
    }

    Qt code is as follows
    QJsonObject name_obj;
    name_obj.insert( "Name", "cat" );

    QJsonObject main_obj;
    main_obj.insert( "General", name_obj ); 
    
    QJsonDocument json_document;
    json_document.setObject( main_obj );
    
    QUrl url = QUrl( "http://***.***.***.***/" );
    
    QString credentials( "username:password" );
    QByteArray data = credentials.toLocal8Bit().toBase64();
    QString auth = "Basic " + data;
    
    QNetworkRequest json_request( url );    
    //json_request.setHeader( QNetworkRequest::ContentTypeHeader, "application/json" );
    json_request.setHeader( QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" );
    json_request.setRawHeader( "Authorization", auth.toLocal8Bit() );
    

    QNetworkAccessManager manager;
    auto reply = manager.post( json_request, json_document.toJson() );
    std::cout << " error " << qPrintable( reply->errorString() ) << std::endl;
    ===>error Unknown error

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

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

    @JoeCFD I am using Qt 5.9.5 on Ubuntu.

    1 Reply Last reply
    0
    • JoeCFDJ JoeCFD

      from command line
      curl -u username:password -d @test.json http://.../
      works fine. The return is
      {
      "OK" : true,
      "Message" : "Configured Successfully"
      }

      test.json:
      {
      "General": {
      "Name": "cat"
      }
      }

      Qt code is as follows
      QJsonObject name_obj;
      name_obj.insert( "Name", "cat" );

      QJsonObject main_obj;
      main_obj.insert( "General", name_obj ); 
      
      QJsonDocument json_document;
      json_document.setObject( main_obj );
      
      QUrl url = QUrl( "http://***.***.***.***/" );
      
      QString credentials( "username:password" );
      QByteArray data = credentials.toLocal8Bit().toBase64();
      QString auth = "Basic " + data;
      
      QNetworkRequest json_request( url );    
      //json_request.setHeader( QNetworkRequest::ContentTypeHeader, "application/json" );
      json_request.setHeader( QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" );
      json_request.setRawHeader( "Authorization", auth.toLocal8Bit() );
      

      QNetworkAccessManager manager;
      auto reply = manager.post( json_request, json_document.toJson() );
      std::cout << " error " << qPrintable( reply->errorString() ) << std::endl;
      ===>error Unknown error

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

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

      @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() );
          });
      

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

      JoeCFDJ 2 Replies Last reply
      1
      • 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 Online
              JonBJ Online
              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 Online
                  JonBJ Online
                  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 Online
                      JonBJ Online
                      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