PDA

View Full Version : QUrl Percent Encoding and addQuery



ttimt
31st March 2015, 05:10
QString q1 = QUrl::toPercentEncoding(username,"","~");

QUrlQuery params;
params.addQueryItem("id", q1);

QString temp1 = params.query();
//temp1 is just testing


When I debug, q1 value is properly encoded.
But for the params and temp1, some are not encoded?






Example:

1. Assigning username with value

"~`!@#$%^&*()_+-=;':"<>?,./"



2. Now q1 will be

"%7E%60%21%40%23%24%25%5E%26%2A%28%29_%2B-%3D%3B%27%3A%22%3C%3E%3F%2C.%2F"
Above 3 characters - _ and . is not encoded and its the correct thing, no problem here.




3. But temp1 shows

"id=~%60%21%40#%24%25%5E%26%2A%28%29_%2B-%3D%3B%27%3A%22%3C%3E%3F%2C.%2F"
Almost same as q1, but ~ and #is not encoded? Why is that



4. Value of params.query id from debugging

"~`%21%40#%24%25^&%2A%28%29_%2B-=%3B%27%3A"<>%3F%2C.%2F"
Many became not encoded, but not ALL. Cause as I know, without using to PercentEncoding, the @ character will not be encoded to %40





I want value of params.query id to be encoded like q1.

As you can see above, I insert q1 into params.addQuery.
But then through debugging the params.query id value and temp1 is different from q1, some characters are not encoded.

I don't use QUrl::FullyEncoded cause it does not actually encode URL reserve characters like @.Tried it
How can I fix this?

wysota
31st March 2015, 07:05
I think you should first construct your query and and then percent-encode it, not the other way round.

ttimt
31st March 2015, 15:40
I think you should first construct your query and and then percent-encode it, not the other way round.
percent encoding query will also encode characters like delimiter, in id = username, = will be %3D.
There is no functions like setRawquery in QUrlQuery.




QString q2 = QUrl::toPercentEncoding(username,"","~");
params.addQueryItem("id", q1);

QString temp2 = params.query(QUrl::FullyEncoded);



q2, fully encoded including ~ and excluding *
temp2 without QUrl::FullyEncoded, encoded all except ~ * and #
temp2 with QUrl::FullyEncoded, encoded all except ~(wrong) and *(correct)

Checking variables when debugging at right side window from this>params>d>itemList>1 show some not encoded, not only ~ * and #.
How?

wysota
31st March 2015, 17:16
Basically ~ and # don't need to be percent encoded. They are valid uri tokens.