PDA

View Full Version : Sending international characters over network



sajmplus
25th October 2016, 17:33
Hello,

i'm trying to send POST request with international characters to my WebAPI.

I'm creating JSON from QString's and try to sent them by QNetworkAccessManager::post method. Unfortunately my WebAPI (it's Java based) rejects QByteArray Utf8 encoding. Our API developer said that my application send JSON UTF8-CodeUnits-Escaped (i.e. "\xc4\x85\xc4\x99" as "Ä…Ä™") however WebAPI accepts only decoded strings (original like "Ä…Ä™") or Unicode code units ("\u0105\u0119").

How can I send post request in Unicode code units? And second question: how can I look into raw QNetworkAccessManager request? (text format like in CURL)

anda_skoa
26th October 2016, 12:37
How did you convert the QString into QByteArray? QString::toUtf8()?

Maybe use QTextCodec with a different encoding? E.g UTF-16?

Cheers,
_

sajmplus
26th October 2016, 12:49
How did you convert the QString into QByteArray? QString::toUtf8()?

Maybe use QTextCodec with a different encoding? E.g UTF-16?

Cheers,
_

Thank you for reply.

I'm get QString from QLineEdit, insert it into QJsonObject as one of JSON value, then generate QByteArray with QJsonDocument::toJson().


#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QString s = "Ä…Ä™";

//Set headers and URL
QNetworkRequest request;
request.setUrl(QUrl("https://web.api.url"));
request.setRawHeader("Accept", "*/*");
request.setRawHeader("Authorization", "Basic **********");
request.setRawHeader("Content-Type", "application/json; charset=utf-8");
request.setRawHeader("Accept-Language", "pl-PL,en;q=0.8");
request.setSslConfiguration(QSslConfiguration::def aultConfiguration());

//Create JsonObject
QJsonObject postJson;
postJson["name"] = s;

QJsonDocument doc(postJson);

//Insert Json to QByteArray
QByteArray postData = doc.toJson(QJsonDocument::Compact);

//Print data step-by-step
qDebug() << "QString:" << s;
qDebug() << "QJsonObject: " << postJson;
qDebug() << "QJsonDocument: " << doc;
qDebug() << "QByteArray: " << postData;

QNetworkAccessManager nam;

//Connect finished signal
QObject::connect(&nam, &QNetworkAccessManager::finished,
[](QNetworkReply *reply) {
qDebug() << "Reply: " << reply->readAll();
qDebug() << "Status code: "
<< reply->attribute(QNetworkRequest::HttpStatusCodeAttribute )
.toInt();
if(!reply->error()) {
qDebug() << "Error: " << reply->errorString();
}
});

//Start POST request
auto reply = nam.post(request, postData);

//Ignore SSL errors
QObject::connect(reply, &QNetworkReply::sslErrors,
[&](const QList<QSslError> &errors) {
reply->ignoreSslErrors(errors);
});

return a.exec();
}

How should I use QTextCodec in my case?

anda_skoa
26th October 2016, 15:21
if "s" is the problematic content than at instead of assigning "s" to the JSON object you assign the converted character array.

Cheers,
_

sajmplus
26th October 2016, 15:33
if "s" is the problematic content than at instead of assigning "s" to the JSON object you assign the converted character array.

Cheers,
_

Ok, but how can I convert this QString to unicode escaped form? Unfortunately I cannot append "\u" character to QString.

Here is console output of my application:


QString: "Ä…Ä™"
QJsonObject: QJsonObject({"name":"Ä…Ä™"})
QJsonDocument: QJsonDocument({"name":"Ä…Ä™"})
QByteArray: "{\"name\":\"\xC4\x85\xC4\x99\"}"
Reply: "{\n \"name\" : \"\xC4\x85\xC4\x99\" \n}"
Status code: 201
Error: "Unknown error"

anda_skoa
26th October 2016, 17:31
How does the assignment look now?
Which text codec did you use?

Cheers,
_

sajmplus
26th October 2016, 17:34
How does the assignment look now?
Which text codec did you use?

Cheers,
_


This output in post #5 is recived from code in post #3.

anda_skoa
26th October 2016, 19:46
Ah, ok.
Report back once you've had a chance to try a different encoding for the value of "name".

Cheers,
_