Encoding a QByteArray into application/x-www-form-urlencoded HTTP POST format?



  • Is there a function/class in QT 4.x (any version) that can take a QByteArray and encode it into the application/x-www-form-urlencoded HTTP POST url format? (with spaces replaced by + signs etc) I dont mind taking it from Webkit or somewhere like that if it exists there.
    I dont need multi-part support or anything like that, just the simple encoding function.

    I found http://code.qt.io/cgit/qt/qt.git/commit/?id=8ad504b31a53ea2a7993f5217c68d0c4aa203b2d but that is code for feeding data in and having it converted to the right format when it gets sent over the network rather than just a simple conversion function (unless I missed something).

    I also found QByteArray::toPercentEncoding but that doesn't encode things the correct way for x-www-form-urlencoded (which needs e.g. spaces to be encoded as + signs and not %20)



  • After some searching of the QT source tree I have (some version of QT 4.7 as it happens) I found a function called FormDataBuilder::encodeStringAsFormData and was able to copy and modify it to do what I needed it to do so and it now works correctly.
    Spaces are encoded as + signs, other things are encoded as the correct % characters and newlines are properly encoded.



  • @jonwil Could you post the solution so future programmers-in-distress can benefit from it?


Log in to reply
 

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