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 948 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.
  • J Offline
    J Offline
    JoeCFD
    wrote on 8 Oct 2020, 14:10 last edited by JoeCFD 10 Aug 2020, 14:12
    #1

    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

    J K 2 Replies Last reply 8 Oct 2020, 14:14
    0
    • J JoeCFD
      8 Oct 2020, 14:10

      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

      J Offline
      J Offline
      JoeCFD
      wrote on 8 Oct 2020, 14:14 last edited by
      #2

      @JoeCFD I am using Qt 5.9.5 on Ubuntu.

      1 Reply Last reply
      0
      • J JoeCFD
        8 Oct 2020, 14:10

        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

        K Offline
        K Offline
        KroMignon
        wrote on 8 Oct 2020, 14:29 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)

        J 2 Replies Last reply 8 Oct 2020, 14:31
        1
        • K KroMignon
          8 Oct 2020, 14:29

          @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() );
              });
          
          J Offline
          J Offline
          JoeCFD
          wrote on 8 Oct 2020, 14:31 last edited by
          #4

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

          1 Reply Last reply
          0
          • K KroMignon
            8 Oct 2020, 14:29

            @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() );
                });
            
            J Offline
            J Offline
            JoeCFD
            wrote on 8 Oct 2020, 16:39 last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • J Offline
              J Offline
              JoeCFD
              wrote on 8 Oct 2020, 16:50 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 K 2 Replies Last reply 8 Oct 2020, 16:58
              0
              • J JoeCFD
                8 Oct 2020, 16:50
                   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 8 Oct 2020, 16:58 last edited by JonB 10 Aug 2020, 17:04
                #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.

                J 1 Reply Last reply 8 Oct 2020, 17:10
                1
                • JonBJ JonB
                  8 Oct 2020, 16:58

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

                  J Offline
                  J Offline
                  JoeCFD
                  wrote on 8 Oct 2020, 17:10 last edited by JoeCFD 10 Aug 2020, 17:12
                  #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 8 Oct 2020, 17:14
                  0
                  • J JoeCFD
                    8 Oct 2020, 17:10

                    @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 8 Oct 2020, 17:14 last edited by JonB 10 Aug 2020, 17:16
                    #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?

                    J 1 Reply Last reply 8 Oct 2020, 17:21
                    0
                    • JonBJ JonB
                      8 Oct 2020, 17:14

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

                      J Offline
                      J Offline
                      JoeCFD
                      wrote on 8 Oct 2020, 17:21 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 8 Oct 2020, 17:28
                      0
                      • J JoeCFD
                        8 Oct 2020, 17:21

                        @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 8 Oct 2020, 17:28 last edited by JonB 10 Aug 2020, 17:32
                        #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.

                        J 1 Reply Last reply 8 Oct 2020, 17:39
                        0
                        • JonBJ JonB
                          8 Oct 2020, 17:28

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

                          J Offline
                          J Offline
                          JoeCFD
                          wrote on 8 Oct 2020, 17:39 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
                          • J JoeCFD
                            8 Oct 2020, 16:50
                               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

                            K Offline
                            K Offline
                            KroMignon
                            wrote on 9 Oct 2020, 05:41 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

                            1/13

                            8 Oct 2020, 14:10

                            • Login

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