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. Encoding problem on reply of QNetworkManager from serverside simple PHP code
Forum Updated to NodeBB v4.3 + New Features

Encoding problem on reply of QNetworkManager from serverside simple PHP code

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 812 Views 1 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.
  • aarelovichA Offline
    aarelovichA Offline
    aarelovich
    wrote on last edited by
    #1

    So I've been doing some tests regarding encondign errors that I've seen around ñ charcater using CURL, Qt and sever side PHP. I've finally got a super minimalistic example where the error is in the Qt side ONLy. Maybe some one can help me out.

    The Qt Code is as follows:

    int main(int argc, char *argv[])
    {
    
        QCoreApplication a(argc, argv);
        QString address = "http://localhost/api_test/test.php";
        QUrl url(address);
        QNetworkAccessManager manager;
        QNetworkRequest request(url);
        QNetworkReply *reply = manager.post(request, QByteArray());
        QObject::connect(reply, &QNetworkReply::finished, QCoreApplication::quit);
        a.exec();
        if(reply->error() == QNetworkReply::NoError){
            qDebug() << "The output";
            qDebug() << QString(reply->readAll()).toUtf8();
        }
        else{
            qDebug() << reply->error() << reply->errorString();
        }
        delete reply;
    
        return 0;
    }
    

    On the server side, test.php is as follows:

    <?php
    
       $data = file_get_contents("uploaded.json");
       echo "$data\n";
    
    ?>
    

    Where "uploaded.json" is a plain text file that contains

    {"name" : "Ariel ñoño", "age" : 58} 
    

    The curl command now works as expected

    ariel@ColoLaptop:/home/web/api_test$ curl http://localhost/api_test/test.php
    {"name" : "Ariel ñoño", "age" : 58} 
    

    But when I run the Qt Application, this happens:

    The output
    "{\"name\" : \"Ariel \xC3\xB1o\xC3\xB1o\", \"age\" : 58} \n\n"
    

    Again the ñ characters get screwed. Can anyone tell me what is wrong with the Qt Code or how can I interpret the returned byte string correctly?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I don't see what's wrong - qDebug() hex-encodes all (possible) non-printable characters.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      aarelovichA 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        I don't see what's wrong - qDebug() hex-encodes all (possible) non-printable characters.

        aarelovichA Offline
        aarelovichA Offline
        aarelovich
        wrote on last edited by
        #3

        @Christian-Ehrlicher Holly Molly!!

        I added this to the code:

        a.exec(); // This is the line from the original code.
        QFile testoutput("test_file.txt");
        if (testoutput.open(QFile::WriteOnly)){
            QTextStream writer(&testoutput);
            writer << QString(reply->readAll()).toUtf8();
            testoutput.close();
        }
        

        And this WORKED!! The file is JUST fine!!! It was a qDebug() issue, apparently.

        Thanks man!!

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @aarelovich said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

          It was a qDebug() issue

          It's not an issue of qDebug() - qDebug() works as expected. You just interpreted the output wrong.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          aarelovichA 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @aarelovich said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

            It was a qDebug() issue

            It's not an issue of qDebug() - qDebug() works as expected. You just interpreted the output wrong.

            aarelovichA Offline
            aarelovichA Offline
            aarelovich
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

            n issue of qDebug() - qDebug() works as expected. You just interpreted the output wrong.

            I don't get it. Is there any way that I can get QDebug() to output the string as I wold read it?

            JonBJ 1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @aarelovich said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

              Is there any way that I can get QDebug() to output the string as I wold read it?

              No since qDebug() is for debugging purposes and has to make sure that the data is printed correctly on all types of output. Therefore every non-ascii character has to be hex-encoded.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              3
              • aarelovichA aarelovich

                @Christian-Ehrlicher said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

                n issue of qDebug() - qDebug() works as expected. You just interpreted the output wrong.

                I don't get it. Is there any way that I can get QDebug() to output the string as I wold read it?

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

                @aarelovich
                Depends how you want to read it :)
                Try:

                qDebug() << qUtf8Printable(whatever);
                

                EDIT
                Actually I think you're already doing that if you're using qDebug() << QString(reply->readAll()).toUtf8();.

                kshegunovK 1 Reply Last reply
                0
                • JonBJ JonB

                  @aarelovich
                  Depends how you want to read it :)
                  Try:

                  qDebug() << qUtf8Printable(whatever);
                  

                  EDIT
                  Actually I think you're already doing that if you're using qDebug() << QString(reply->readAll()).toUtf8();.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @JonB said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

                  Actually I think you're already doing that if you're using qDebug() << QString(reply->readAll()).toUtf8();.

                  qDebug().noquote() << "some strange text";
                  

                  The problem isn't the encoding, QDebug escapes stuff by default, as it should be.

                  @aarelovich said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

                  I don't get it. Is there any way that I can get QDebug() to output the string as I wold read it?

                  Look above.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  2
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @kshegunov said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

                    Look above.

                    As already said otherwise - this is a hack and misuse of qDebug() and only works when the console supports the used locale. qDebug() is for debugging purposes only, nothing more.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    kshegunovK 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @kshegunov said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

                      Look above.

                      As already said otherwise - this is a hack and misuse of qDebug() and only works when the console supports the used locale. qDebug() is for debugging purposes only, nothing more.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

                      As already said otherwise - this is a hack and misuse of qDebug() and only works when the console supports the used locale. qDebug() is for debugging purposes only, nothing more.

                      It's no hack, as it is part of the public API. It is useful sometimes to see the raw output, limitations of the attached terminal aside.

                      Read and abide by the Qt Code of Conduct

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • kshegunovK kshegunov

                        @Christian-Ehrlicher said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

                        As already said otherwise - this is a hack and misuse of qDebug() and only works when the console supports the used locale. qDebug() is for debugging purposes only, nothing more.

                        It's no hack, as it is part of the public API. It is useful sometimes to see the raw output, limitations of the attached terminal aside.

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @kshegunov said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:

                        It's no hack

                        I meant it's a hack to modify a QString in any way to see non-ascii characters as not hex-encoded. It works as long as the terminal supports it which will not be the case e.g. on windows.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        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