Network Manager automatically replaces character



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


  • Lifetime Qt Champion

    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.



  • @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\",\"**\":\"**\",\"***\":\"*****\"}}"
    


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


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



  • Thanks for the help


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.