Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Network Manager automatically replaces character

    General and Desktop
    3
    6
    1016
    Loading More Posts
    • 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.
    • G
      goocreations last edited by goocreations

      I'm using QNetworkAccessManager to send JSON data to my server using POST. Qt automatically replaces the + sign with a space in my POST data. I can't see why this should happen. With GET parameters I could understand that symbols are URL encoded (although a space is not the URL encoded version of +), but I can't understand why Qt would replace any characters in my POST data.

      I first encountered this problem with base64 data, but now the same happens if I send a phone number in international format (eg: +49 123 45678). This is NOT due to my encoding, just before manager->post(json), the + sign is still present, but when the json is arrives on the server or if I inspect the POST data on the client before it leaves, the + sign is replaced, hence QNetworkAccessManager must be doing something.

      Is there a reason why this is done and is there any way to switch this off?

      the_ 1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Can you share the code you use to build and send your JSON data ? Also, please post what you get and what you expect.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • the_
          the_ @goocreations last edited by the_

          @goocreations

          As far as I remember, in GET requests the "+" is the replacement for a space " ". Seems that this is also for POST requests.
          To send a "+" you have to replace it with %<hex value> in this case its %2B

          //EDIT example added

          
          h2.insert("Debug","1%2B3+4");
          //some more data inserted in h1 and h2, masked with * in the debug output from my side
          h1.insert("params",h2);
          jdoc = QJsonDocument::fromVariant(h1);
          query.addQueryItem("str",jdoc.toJson(QJsonDocument::Compact));
          qDebug() << query.toString(QUrl::FullyEncoded);
          
          //in the QNetworkReply::finished() slot:
          QString retval = rep->readAll();
          qDebug() << retval;
          

          reads in the debug output as

          "str=%7B%22****%22:%22***%22,%22params%22:%7B%22Debug%22:%221%2B3+4%22,%22***%22:%22**%22,%22***%22:%22****%22%7D%7D"
          
          \"Debug\":{\"Debug\":\"1+3 4\",\"**\":\"**\",\"***\":\"*****\"}}"
          

          -- No support in PM --

          1 Reply Last reply Reply Quote 0
          • G
            goocreations last edited by goocreations

            @the_

            Thank you, using the hex value worked. 2 questions:

            1. Why does Qt touch the post data, I don't see a reason for the post data to be changed. Is this a bug or am I missing something?
            2. Do you know of any other characters that are replaced as well?
            the_ 1 Reply Last reply Reply Quote 0
            • the_
              the_ @goocreations last edited by the_

              @goocreations

              ad 1)
              Its not Qt that changes the "+" sign. As you can see in the example, the first debug output is the encoded post parameter and it still contains the "+".
              ad 2)
              I dont know if there are any other special characters, just check the HTTP standard for this
              //EDIT RFC added
              Reserved Characters in URL

              -- No support in PM --

              1 Reply Last reply Reply Quote 0
              • G
                goocreations last edited by

                Thanks for the help

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post