Results 1 to 8 of 8

Thread: Sending international characters over network

  1. #1

    Default Sending international characters over network

    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:ost 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)

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending international characters over network

    How did you convert the QString into QByteArray? QString::toUtf8()?

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

    Cheers,
    _

  3. #3

    Default Re: Sending international characters over network

    Quote Originally Posted by anda_skoa View Post
    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().
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QJsonDocument>
    3. #include <QJsonObject>
    4. #include <QNetworkAccessManager>
    5. #include <QNetworkReply>
    6. #include <QNetworkRequest>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11.  
    12. QString s = "Ä…Ä™";
    13.  
    14. //Set headers and URL
    15. QNetworkRequest request;
    16. request.setUrl(QUrl("https://web.api.url"));
    17. request.setRawHeader("Accept", "*/*");
    18. request.setRawHeader("Authorization", "Basic **********");
    19. request.setRawHeader("Content-Type", "application/json; charset=utf-8");
    20. request.setRawHeader("Accept-Language", "pl-PL,en;q=0.8");
    21. request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
    22.  
    23. //Create JsonObject
    24. QJsonObject postJson;
    25. postJson["name"] = s;
    26.  
    27. QJsonDocument doc(postJson);
    28.  
    29. //Insert Json to QByteArray
    30. QByteArray postData = doc.toJson(QJsonDocument::Compact);
    31.  
    32. //Print data step-by-step
    33. qDebug() << "QString:" << s;
    34. qDebug() << "QJsonObject: " << postJson;
    35. qDebug() << "QJsonDocument: " << doc;
    36. qDebug() << "QByteArray: " << postData;
    37.  
    38. QNetworkAccessManager nam;
    39.  
    40. //Connect finished signal
    41. QObject::connect(&nam, &QNetworkAccessManager::finished,
    42. [](QNetworkReply *reply) {
    43. qDebug() << "Reply: " << reply->readAll();
    44. qDebug() << "Status code: "
    45. << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)
    46. .toInt();
    47. if(!reply->error()) {
    48. qDebug() << "Error: " << reply->errorString();
    49. }
    50. });
    51.  
    52. //Start POST request
    53. auto reply = nam.post(request, postData);
    54.  
    55. //Ignore SSL errors
    56. QObject::connect(reply, &QNetworkReply::sslErrors,
    57. [&](const QList<QSslError> &errors) {
    58. reply->ignoreSslErrors(errors);
    59. });
    60.  
    61. return a.exec();
    62. }
    To copy to clipboard, switch view to plain text mode 

    How should I use QTextCodec in my case?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending international characters over network

    if "s" is the problematic content than at instead of assigning "s" to the JSON object you assign the converted character array.

    Cheers,
    _

  5. #5

    Default Re: Sending international characters over network

    Quote Originally Posted by anda_skoa View Post
    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"

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending international characters over network

    How does the assignment look now?
    Which text codec did you use?

    Cheers,
    _

  7. #7

    Default Re: Sending international characters over network

    Quote Originally Posted by anda_skoa View Post
    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending international characters over network

    Ah, ok.
    Report back once you've had a chance to try a different encoding for the value of "name".

    Cheers,
    _

Similar Threads

  1. Sending HTTP POST network request
    By Ashley in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2015, 21:41
  2. Sending commands throught network
    By franco.amato in forum Newbie
    Replies: 1
    Last Post: 26th March 2012, 22:33
  3. International Phometic Alphabet (IPA) characters
    By handle0088 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 5th April 2011, 06:07
  4. sending Text or binary file over network
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2008, 00:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.