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. Network Manager automatically replaces character
Forum Updated to NodeBB v4.3 + New Features

Network Manager automatically replaces character

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.4k Views 2 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.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by goocreations
    #1

    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_T 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • G 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_T Offline
        the_T Offline
        the_
        wrote on last edited by the_
        #3

        @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
        0
        • G Offline
          G Offline
          goocreations
          wrote on last edited by goocreations
          #4

          @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_T 1 Reply Last reply
          0
          • G 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_T Offline
            the_T Offline
            the_
            wrote on last edited by the_
            #5

            @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
            0
            • G Offline
              G Offline
              goocreations
              wrote on last edited by
              #6

              Thanks for the help

              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