QString convert accent error
-
Hi everybody,
I have a problem with a conversion of my string when I send a request to a server. I'd like to send a file with the name "test_arrière.mp4", but Qt converts the character "è" with an hexa value, so the server send me an error :
Malformed input or input contains unmappable characters: test_arri\xC3\xA8re
How can I disable this conversion ? I tried different method of QString (fromUtf8, toLatin1, etc...) but I don't understand how to do it.
NB : The HTTP header part which have the problem is QNetworkRequest::ContentDispositionHeader -> QVariant("form-data; name="file"; filename="test_arrière.mp4")
Thanks
-
@OPit said in QString convert accent error:
so how I convert my variable : toUtf8, toLatin1, ... ?
It depends on what you want - if you need utf8() I would guess .toLatin1() is not what you want, or?
-
filename=QString::fromUtf8(localFile.toUtf8) or filename=QString::fromUtf8(localFile.toLatin1()) (with localFile = "test_arrière.mp4") give me the same error from server :
response = "{\n \"timestamp\" : \"03-02-2020 09:35:32\",\n \"status\" : 500,\n \"error\" : \"Internal Server Error\",\n \"exception\" : \"java.nio.file.InvalidPathException\",\n \"message\" : \"Malformed input or input contains unmappable characters: test_arri\xC3\xA8re.mp4\",\n \"path\" : \"/api/file/manual\"\n}"
Here some examples of qDebug() outputs of my variable, why I can't see the good format output ??? :
direct print "test_arrière.mp4" toUtf8 print "test_arri\xC3\xA8re.mp4" toLatin1 print "test_arri\xE8re.mp4" fromUtf8(file.toUtf8()) print "test_arrière.mp4" fromUtf8(file.toLatin1()) print "test_arri�re.mp4"
-
So what exactly are you trying to achieve - where (and in which format) do you send your request to, what encoding does it need, how / from where to you read the filename?
-
@OPit said in QString convert accent error:
Here some examples of qDebug() outputs of my variable, why I can't see the good format output ??? :
Can you try?
qDebug() << qUtf8Printable(localFile);
And actually, what do you want to achieve? Have you trouble with JSon request building?
-
@Christian-Ehrlicher
I read a list of local files and I want to send them to a distant server that I don't really know what formats it can read.local file -> QDir::entryList () -> send to server via QNetworkAccessManager->post and QHttpMultiPart/QHttpPart objects -> server
I've shown you the result of qDebug() because I don't know if the "mistake" come from entryList or after. Why I can't see the character 'è' when I just make a qDebug() << file ?
@KroMignon :
the result is the same : qUtf8Printable(localFile) => test_arrière.mp4I just want to send file to a server :(, but it appears that the server doesn't accept my filename in
ContentDispositionHeader field because of utf8 encoded character ... -
@OPit said in QString convert accent error:
ContentDispositionHeader field because of utf8 encoded character ...
So as I already said - you have to find out what encoding the server accepts. If it is not UTF-8 you can't send this filename.
-
filename=QString::fromUtf8(localFile.toUtf8)
that is nonsense.
filename=QString::fromUtf8(localFile.toLatin1())
that is even more nonsense.
First you have to check if the
QString
itself is correct, e.g. by printing it to a label:ui->label.setText(localFile);
Is that giving correct output?
Regards
-
@OPit said in QString convert accent error:
ContentDispositionHeader field because of utf8 encoded character
Yes, this is clearly your problem... What is you server await as string encoding type? Qt will always work in UTF8 internally, but you can transform this UTF8 string in to whatever you need.
For example, in your Terminal, you seed "test_arrière.mp4", because you are showing an UTF8 string in you terminal which, I supposed is windows terminal. This should work:
QTextCodec *codec = QTextCodec::codecForName("IBM850"); qDebug() << codec->fromUnicode(localFile);
So you have to known which kind of text encoding you server is using.
-
Thanks a lot, I understand.
The last thing. It's very strange. When I run the code below on my main file (encoded to utf8) :
QStringList files = QDir("D:/temp").entryList(QStringList() << "*.mp4"); QTextCodec * codec = QTextCodec::codecForLocale(); qDebug() << codec->toUnicode(file[0].toLatin1()); --> test_arrière.mp4
I have the good result.
But when I run the same code in my transfer class (also encoded to utf8), the result is wrong ???
--> test_arrière.mp4
Why ?
-
@OPit said in QString convert accent error:
But when I run the same code in my transfer class (also encoded to utf8), the result is wrong ???
Again: how do you add it to the http request, does your server understand the encoding you send?
What you're doing with qDebug() works by accident and is completely nonsense.