Search code examples
c++qtencodingurl-encodingqstring

QUrl toPercentEncoding() lowercase hex


I have a string that becomes the query string part of a url and has to be url encoded.

QString queryStringParam = "J1mOEfg/TC";

So, I tried to use QUrl::toPercentEncoding() like this

QString encodedQueryString = QUrl::toPercentEncoding(queryStringParam);

which results in "J1mOEfg%2FT" but the webservice that I'm calling is expecting "J1mOEfg%2fT" [note the lowercase 'f' in the encoding of '/' to '%20f'] and hence rejects the parameter. It's probably bad on the service's side but I can't do anything about it. How can I get lowercase encoding hex chars while maintaining the case in the rest of the parameter?


Solution

  • I don't think you can do it with standard functions, but you can easily write your own. In percent-encoded strings special characters are encoded with a % symbol that is followed by a pair of hex digits. Knowing that, you can write a function like this:

    QString lowerPercentEncoding( QString str )
    {
        int index = str.indexOf("%");
        while( index != -1 )
        {
            QString tmp = str.mid(index, 3);
            str.replace(index,3,tmp.toLower());
            index = str.indexOf("%", index + 1);
        }
        return str;
    }