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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With