Encoding problem on reply of QNetworkManager from serverside simple PHP code
-
So I've been doing some tests regarding encondign errors that I've seen around ñ charcater using CURL, Qt and sever side PHP. I've finally got a super minimalistic example where the error is in the Qt side ONLy. Maybe some one can help me out.
The Qt Code is as follows:
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString address = "http://localhost/api_test/test.php"; QUrl url(address); QNetworkAccessManager manager; QNetworkRequest request(url); QNetworkReply *reply = manager.post(request, QByteArray()); QObject::connect(reply, &QNetworkReply::finished, QCoreApplication::quit); a.exec(); if(reply->error() == QNetworkReply::NoError){ qDebug() << "The output"; qDebug() << QString(reply->readAll()).toUtf8(); } else{ qDebug() << reply->error() << reply->errorString(); } delete reply; return 0; }On the server side, test.php is as follows:
<?php $data = file_get_contents("uploaded.json"); echo "$data\n"; ?>Where "uploaded.json" is a plain text file that contains
{"name" : "Ariel ñoño", "age" : 58}The curl command now works as expected
ariel@ColoLaptop:/home/web/api_test$ curl http://localhost/api_test/test.php {"name" : "Ariel ñoño", "age" : 58}But when I run the Qt Application, this happens:
The output "{\"name\" : \"Ariel \xC3\xB1o\xC3\xB1o\", \"age\" : 58} \n\n"Again the ñ characters get screwed. Can anyone tell me what is wrong with the Qt Code or how can I interpret the returned byte string correctly?
-
I don't see what's wrong - qDebug() hex-encodes all (possible) non-printable characters.
-
I don't see what's wrong - qDebug() hex-encodes all (possible) non-printable characters.
@Christian-Ehrlicher Holly Molly!!
I added this to the code:
a.exec(); // This is the line from the original code. QFile testoutput("test_file.txt"); if (testoutput.open(QFile::WriteOnly)){ QTextStream writer(&testoutput); writer << QString(reply->readAll()).toUtf8(); testoutput.close(); }And this WORKED!! The file is JUST fine!!! It was a qDebug() issue, apparently.
Thanks man!!
-
@aarelovich said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
It was a qDebug() issue
It's not an issue of qDebug() - qDebug() works as expected. You just interpreted the output wrong.
-
@aarelovich said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
It was a qDebug() issue
It's not an issue of qDebug() - qDebug() works as expected. You just interpreted the output wrong.
@Christian-Ehrlicher said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
n issue of qDebug() - qDebug() works as expected. You just interpreted the output wrong.
I don't get it. Is there any way that I can get QDebug() to output the string as I wold read it?
-
@aarelovich said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
Is there any way that I can get QDebug() to output the string as I wold read it?
No since qDebug() is for debugging purposes and has to make sure that the data is printed correctly on all types of output. Therefore every non-ascii character has to be hex-encoded.
-
@Christian-Ehrlicher said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
n issue of qDebug() - qDebug() works as expected. You just interpreted the output wrong.
I don't get it. Is there any way that I can get QDebug() to output the string as I wold read it?
@aarelovich
Depends how you want to read it :)
Try:qDebug() << qUtf8Printable(whatever);EDIT
Actually I think you're already doing that if you're usingqDebug() << QString(reply->readAll()).toUtf8();. -
@aarelovich
Depends how you want to read it :)
Try:qDebug() << qUtf8Printable(whatever);EDIT
Actually I think you're already doing that if you're usingqDebug() << QString(reply->readAll()).toUtf8();.@JonB said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
Actually I think you're already doing that if you're using qDebug() << QString(reply->readAll()).toUtf8();.
qDebug().noquote() << "some strange text";The problem isn't the encoding,
QDebugescapes stuff by default, as it should be.@aarelovich said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
I don't get it. Is there any way that I can get QDebug() to output the string as I wold read it?
Look above.
-
@kshegunov said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
Look above.
As already said otherwise - this is a hack and misuse of qDebug() and only works when the console supports the used locale. qDebug() is for debugging purposes only, nothing more.
-
@kshegunov said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
Look above.
As already said otherwise - this is a hack and misuse of qDebug() and only works when the console supports the used locale. qDebug() is for debugging purposes only, nothing more.
@Christian-Ehrlicher said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
As already said otherwise - this is a hack and misuse of qDebug() and only works when the console supports the used locale. qDebug() is for debugging purposes only, nothing more.
It's no hack, as it is part of the public API. It is useful sometimes to see the raw output, limitations of the attached terminal aside.
-
@Christian-Ehrlicher said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
As already said otherwise - this is a hack and misuse of qDebug() and only works when the console supports the used locale. qDebug() is for debugging purposes only, nothing more.
It's no hack, as it is part of the public API. It is useful sometimes to see the raw output, limitations of the attached terminal aside.
@kshegunov said in Encoding problem on reply of QNetworkManager from serverside simple PHP code:
It's no hack
I meant it's a hack to modify a QString in any way to see non-ascii characters as not hex-encoded. It works as long as the terminal supports it which will not be the case e.g. on windows.