Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt does not encode '+' sign

Tags:

encoding

qt

Why doesn't Qt5 encode plus sign to %2B?

I've tried this code:

QUrlQuery urlQuery;
urlQuery.addQueryItem("test", "hello+world");
manager->post(request, urlQuery.toString(QUrl::FullyEncoded).toUtf8());

But on server's side I always get string like:

hello world

without %2B and without plus sign. So I can't get a '+' sign on the server side...

How can I send a plus sign with Qt5?

like image 525
Mike Avatar asked Apr 11 '16 14:04

Mike


1 Answers

How can I send a plus sign with Qt5?

Don't use an url for post data, use a QByteArray. It will be sent as is.

EDIT

Additional info for QUrlQuery, the + sign is a special case (from the doc):

Handling of spaces and plus ("+")

Web browsers usually encode spaces found in HTML FORM elements to a plus sign ("+") and plus signs to its percent-encoded form (%2B). However, the Internet specifications governing URLs do not consider spaces and the plus character equivalent. For that reason, QUrlQuery never encodes the space character to "+" and will never decode "+" to a space character. Instead, space characters will be rendered "%20" in encoded form. To support encoding like that of HTML forms, QUrlQuery also never decodes the "%2B" sequence to a plus sign nor encode a plus sign. In fact, any "%2B" or "+" sequences found in the keys, values, or query string are left exactly like written (except for the uppercasing of "%2b" to "%2B").

So if you want to use QUrlQuery for strings containing + signs, it seems you have to do the encoding yourself ("+" => "%2B"), you can use the static method QUrl::toPercentEncoding() for that.

like image 200
Ilya Avatar answered Sep 28 '22 06:09

Ilya