QUrl encoding problem (Qt 5.0.2)
-
NOTE: It looks like this post may not display properly on the forum, raw text of it is here: http://codepad.org/ykMXCtXN
Hey All,
I've been banging my head against the wall with this issue for hours, hoping someone here has a workaround!Is there a way to set a QUrl without it being 'smart' and automatically messing with the URL encoding? This default behaviour makes it impossible to form a valid request.
I am using a rest API that requires me to POST to a URL which contains a path+filename. Here is my attempt to upload a file named "first;second.jpg"
I construct a QByteArray that has the encoding I want, and print it out:
@QByteArray rawUrl;
...
qDebug() << "as QByteArray" << rawUrl;OUTPUT: as QByteArray "https://www.example.com/0/io/init_upload/%2FDesktop Uploads%2Fsemicolons%2Ffirst%3Bsecond.jpg"@
As you can see the semicolon is nicely encoded as a %3B
Then I construct a QUrl from it:
@QUrl url(rawUrl); // using url.setUrl(rawUrl, QUrl::StrictMode) has same effect
Q_ASSERT(url.isValid()); // succeeds
qDebug() << "as QUrl " << url.toEncoded(QUrl::FullyEncoded);OUTPUT: as QUrl "https://www.example.com/0/io/init_upload/%2FDesktop Uploads%2Fsemicolons%2Ffirst;second.jpg"@
As you can see the semicolon was decoded. When I create a QNetworkRequest with this URL, and observe what it is doing on the network with Charles Proxy I can see the request being made exactly matches that debug output.
The expected behaviour is that the semicolon remains as %3B
(This request fails because ; is just like ? or & and cuts the URL short as if arguments were to follow, and they arent. ugh.) -
(on a related note, I had to URL encode this entire post for it to show up correctly. Looks like this forum automagically decodes things as well! haha dang.)
-
The forum ate this post, here is the RAW text: http://codepad.org/ykMXCtXN
-
To paraphrase.
Is it possible to create a QUrl that contains %3B (percent-three-be) in it? Not a semicolon, but that exact sequence of three characters.
I cannot find a way to do this.
-
@
QUrl url;
url.setScheme("https");
url.setHost("www.example.com");
url.setEncodedPath("0/io/init_upload/%2FDesktop%20Uploads%2Fsemicolons%2Ffirst%3Bsecond.jpg");
qDebug() << url.toEncoded();url = QUrl::fromEncoded("https://www.example.com/0/io/init_upload/%2FDesktop Uploads%2Fsemicolons%2Ffirst%3Bsecond.jpg");
qDebug() << url.toEncoded();
@
Outputs:
@
"https://www.example.com/0/io/init_upload/%2FDesktop Uploads%2Fsemicolons%2Ffirst%3Bsecond.jpg"
"https://www.example.com/0/io/init_upload/%2FDesktop Uploads%2Fsemicolons%2Ffirst%3Bsecond.jpg"
@ -
That looks sound, but unfortunately setEncodedPath() doesn't seem to exist in QT 5.0.2 anymore :(
@qurl.h
...
QT_DEPRECATED void setEncodedPath(const QByteArray &value)
{ setPath(fromEncodedComponent_helper(value)); }
...
@Is there a way to access the deprecated function, or something new in 5 that has replaced it?
-
Hmmm, nice change of behaviour. I'll have to give it some thought.
-
This is still an issue with no apparent way to work around.
How should I got about filing a bug / making a request?
-
Hi,
Even I am facing similar issue, in my case colon (:) is getting decoded by default.
I found this ticket https://bugreports.qt-project.org/browse/QTBUG-31660 logged for this issue, but it does not say anything about when the fix will be available.